Categories
JavaScript Answers

How to find the index of an object whose attributes match a search within an array of JavaScript objects?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.