remove undefined from object javascript 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}
Example 4: delete object property vs undefined assignment javascript
a.x = undefined
a.hasOwnProperty("x")
delete a.x
a.hasOwnProperty("x")