Manju Kb Manju Kb - 2 months ago 10
Javascript Question

How javascripts indexOfKey works

I have an array object like this

var x = [{"_id":null,"count":7},{"_id":false,"count":362}, {"_id":true,"count":926}]


How to take index of _id = false; object

tried this
x.indexOfKey(false, "_id")
but returns -1
and this works fine
x.indexOfKey(true, "_id")


What am i doing wrong??

Answer

Use the standard array.findIndex API.

See:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

var x =  [{"_id":null,"count":7},{"_id":false,"count":362}, {"_id":true,"count":926}];
console.log("Index of _id==false is: " + x.findIndex((element) => { return element._id == false; }));