How to Get Index of an Object in Array in JavaScript

To get an index of an Object in Array in JavaScript:

  1. Using the Array.prototype.findIndex() method.
  2. Using the “lodash” Library to get the index of an object in an array in JavaScript.

Using Array.prototype.findIndex() function

The findIndex() is a built-in JavaScript array method that returns the index of the first element in the array that satisfies the provided testing function. The findIndex() method returns the index of the first element that passed the provided condition. Like most other programs like c++ and java, it returns -1 if no element can fulfill the provided condition.

If the findIndex() method returns -1, that means no element passed the test. ECMAScript 2015 (ES6) introduced a new method called the findIndex() method to the Array.prototype. 

It stops execution after returning the first element that fulfills the provided condition. So if there are multiple duplicate elements in the array, it will return the index of the first occurrence of the element.

Syntax

array.findIndex(function(currValue,index, arr));

Arguments

Here, the “index” and “arr” are optional arguments that can be passed to the callback function. The “Index” argument points to the current element’s index, and arr is the array object to which the current element belongs.

The array.findIndex() function returns the index of the first matched element or returns -1 if no element can fulfill the condition.

Return value

It returns the x (where x is a single integer).

Example

const marks = [30, 100, 17, 23, 52, 20];
let index = marks.findIndex(marks => marks > 90);
console.log(index);

Output

1

First, we created an array using constant keywords. marks here is the name of a variable that is assigned by us(the user). We then created another array using the let keyword. The variable name is an index. Again this is a user-defined variable.

Now we used the function called findIndex(). Within the function, we have set a condition. Here we basically need to find the index of the first occurrence of the element whose marks are strictly greater than 90.

In the given array 100 is an element that satisfies the condition and its index is 1. Hence the variable index got assigned the value of 1. At last, using the console.log() function we printed the index variable.

Let’s see another example.

const Employee = [
   {
      name: 'Diwakar',
      company: 'Amazon',
   },
   {
      name: 'Veenus',
      company: 'Deshaw',
   },
   {
      name: 'Akanksha',
      company: 'TCS',
   },
   {
      name: 'Saumya',
      company: 'Infosys',
   }, 
]

const index = Employee.findIndex((element) => element.company === 'Amazon');
console.log(index)

Output

0

Example of When index is not found

const marks = [887, 5, 7, 5, 7, 5, 4, 44, 45, 447, 296, 8];
let index = marks.findIndex(marks => marks > 1000);
console.log(index);

Output

-1

In the above code, we instructed the program to assign the value of the index of the element of the array that is more than 1000. But no element in the array is greater than 1000. As a default behavior therefore Javascript will return -1. So the index got -1 as the assigned value.

Using the “lodash” library to get the index of an object in an array

In JavaScript, the lodash is a lightweight library, which helps to code easily by simplifying working with arrays, numbers, objects, strings, etc. We can easily download the lodash library from its official website and can use it on top of our website.

To install loadash in JavaScript, use the following code.

npm install loadash --save

Syntax

_.findIndex(array, [predicate=_.identity], [fromIndex=0])

Arguments

The “array” is the array that we need to process.

The [predicate=_.identity] is a function that is invoked at each iteration.

The [fromIndex=0] it’s an optional parameter that sets the starting point.

Example

To import the loadash library in a JavaScript file, use the import statement.

Now, add the following code inside the app.js file.

import _ from "lodash"

const Employee = [
  {
    name: 'Diwakar',
    company: 'Amazon',
  },
  {
    name: 'Veenus',
    company: 'Deshaw',
  },
  {
    name: 'Akanksha',
    company: 'TCS',
  },
  {
    name: 'Saumya',
    company: 'Infosys',
  },
]

var index = _.findIndex(Employee, { company: 'TCS' })
console.log(index);

Output

2

In this example, we are checking an array filled with objects and specifically checking company: TCS and it finds then it returns its index which is 2 and if it does not find then it will return -1.

That’s it for this tutorial.

See also

How to Convert Object to Array of Objects in JavaScript

How to Add Property to Array of Objects in JavaScript

How to flatten Array of Objects in JavaScript

How to flatten Array of Objects in JavaScript

How to Check If Object is Empty in JavaScript

Leave a Comment