trim undefined keys from object typescript code example

Example 1: js remove undefined from object

Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key])

Example 2: trim undefined keys from object typescript

const obj = {
  a: 1,
  b: undefined
}

// pure functional approach without side-effects
const newObject = Object.keys(obj).reduce((acc, key) => {
  const _acc = acc;
  if (obj[key] !== undefined) _acc[key] = obj[key];
  return _acc;
}, {})

console.log(newObject)
// Object {a: 1}