remove undefined values from object javascript 1

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}




Here is what the above code is Doing:
1. JSON.stringify(obj) converts the object into a string.
2. JSON.parse(str) converts the string back into an object.
3. The stringified object doesn’t have the undefined property, so when it’s converted back into an object, that property is gone.

Similar Posts