remove undefined from object js code example
Example 1: js remove undefined from object
Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key])
Example 2: remove undefined from object js
const buildAnObjectFromAQuery = query => (
{
...query.foo && { foo: query.foo },
...query.bar && { bar: query.bar },
}
);
Example 3: remove undefined values from object javascript
var obj = { a: 1, b: undefined, c: 3 }
console.log(JSON.parse(JSON.stringify(obj)));
Output: {a: 1, c: 3}