js remove undefined from object trim undefined keys from object typescript

The solution for “js remove undefined from object trim undefined keys from object typescript” can be found here. The following code will assist you in solving the problem.

Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key])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}

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in