findIndex array of oBJECTS code example
Example 1: javascript findindex
const array1 = [5, 12, 8, 130, 44];
const search = element => element > 13;
console.log(array1.findIndex(search));
// expected output: 3
const array2 = [
{ id: 1, dev: false },
{ id: 2, dev: false },
{ id: 3, dev: true }
];
const search = obj => obj.dev === true;
console.log(array2.findIndex(search));
// expected output: 2
Example 2: find the index of an object in an array
const letters = [{letter: 'a'},{letter: 'b'},{letter: 'c'}]
const index = letters.findIndex((element, index) => {
if (element.letter === 'b') {
return true
}
})
//index is `1`