Categories
JavaScript Answers

How to find index of an object by key and value in a JavaScript array?

Spread the love

To find index of an object by key and value in a JavaScript array, we use the findIndex method.

For instance, we write

const index = peoples.findIndex((p) => p.attr1 == "john");

to call peoples.findIndex with a callback that checks if attr1 property of each object in peoples is 'john'.

The first index of the found item is returned.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published. Required fields are marked *