EN
JavaScript - How to remove empty object from array?
1
answers
0
points
I have an array of objects and I want to remove empty objects from it:
const array = [{ id: 1 }, {}, {}, {}];
How can I do this?
1 answer
0
points
Use filter() method with Object.keys() to find and remove all objects that doesn't contain any key.
Practical example:
// ONLINE-RUNNER:browser;
const array = [{ id: 1 }, {}, {}, {}];
const result = array.filter((value) => Object.keys(value).length !== 0);
console.log(JSON.stringify(result)); // [ { id: 1 } ]
References
0 comments
Add comment