How to find index of empty object in array of object
You can make use of findIndex
and Object.keys()
:
const arr = [{a:'b'}, {}];
const emptyIndex = arr.findIndex((obj) => Object.keys(obj).length === 0);
console.log(emptyIndex);
If you search for an empty object, you search for the same object reference of the target object.
You could search for an object without keys instead.
var array = [{ a: 'b' }, {}] ,
index = array.findIndex(o => !Object.keys(o).length);
console.log(index);