Navigate back to the homepage

How to find the index of an object in an array in javascript?

Infinitbility
JavaScript
December 22nd, 2021 · 1 min read
How to find the index of an object in an array in javascript?

Hello Friends 👋,

Welcome to Infinitbility.

Today, we are going to learn how we can get index of object based array on object property condition if condition true we should get index of match object.

Javascript provide findIndex() method to get index of element in array, we can also use this method to find index of object in array.

findIndex() method return -1 if condition not matched.

Supppose we have array of objects like below.

1let Users = [
2 {
3 id: 1,
4 name: 'infinitbility',
5 slogan: 'We have the ability to build infinite way for us.',
6 dob: '2021-07-26',
7 isActive: 1,
8 },
9 {
10 id: 2,
11 name: 'atom',
12 slogan: 'Rebuild Atom',
13 dob: '2001-02-16',
14 isActive: 1,
15 },
16];

Now, we are going use findIndex() method with condition to get index of object.

For find index array of objects, we should follow below syntax.

1Users.findIndex(e => e.id == 2);

In below example, i have added example of if findIndex method got true condition or false both.

1let Users = [
2 {
3 id: 1,
4 name: 'infinitbility',
5 slogan: 'We have the ability to build infinite way for us.',
6 dob: '2021-07-26',
7 isActive: 1,
8 },
9 {
10 id: 2,
11 name: 'atom',
12 slogan: 'Rebuild Atom',
13 dob: '2001-02-16',
14 isActive: 1,
15 },
16];
17
18// found index example
19Users.findIndex(e => e.id == 2);
20
21// not found index example
22Users.findIndex(e => e.id == 3);

Output

Javascript findIndex() Example
Javascript findIndex() Example

Thanks for reading…

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to get all keys of object in javascript?

How to get all keys of object in javascript?

Javascript, Get array of object keys using javascrit `Object.keys()` method.

December 21st, 2021 · 1 min read
How to assign object without reference in javascript?

How to assign object without reference in javascript?

Javascript, object assigning without reference example.

December 20th, 2021 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]