indexof javascript array of objects code example
Example 1: javascript indexOf object value in array
const needle = 3;
const haystack = [{ id: 1 }, { id: 2 }, { id: 3 }];
const index = haystack.findIndex(item => item.id === needle);
Example 2: using index of on array of objects
arr = [{x: "a", y: 1}, {x: "something", y: 2}]
arr.findIndex(obj => obj.x === "a" && obj.y === 1);
Example 3: index of value in array
var imageList = [
{value: 100},
{value: 200},
{value: 300},
{value: 400},
{value: 500}
];
var index = imageList.findIndex(img => img.value === 200);
Example 4: indexof object javascript
var hello = {
hello: 'world',
foo: 'bar'
};
var qaz = {
hello: 'stevie',
foo: 'baz'
}
var myArray = [];
myArray.push(hello,qaz);
var indexOfStevie = myArray.findIndex(i => i.hello === "stevie");