How can I remove empty object in from an array in JS How can I remove empty object in from an array in JS jquery jquery

How can I remove empty object in from an array in JS


var newArray = array.filter(value => Object.keys(value).length !== 0);


You can use Array.prototype.filter to remove the empty objects before stringifying.

JSON.stringify(array.filter(function(el) {    // keep element if it's not an object, or if it's a non-empty object    return typeof el != "object" || Array.isArray(el) || Object.keys(el).length > 0;});


I would recommend using the following code:

var newArray = array.filter(value => JSON.stringify(value) !== '{}');

I did not use Object.keys(value).length !== 0 because it not only removes empty objects { } but also removes empty arrays [ ]. If you only want to remove empty objects, use the above method.