To find the index of an object whose attributes match a search within an array of JavaScript objects, we use the findIndex
method.
For instance, we write
const item = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }].findIndex(
(obj) => obj.id == 3
);
to call findIndex
on the [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]
array to find the index of the item with the id
property equal to 3.