How to Get Index of an Object in Array in JavaScript

There are the following ways to get an index of an Object in a JavaScript array.

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

Method 1: Using Array.prototype.findIndex() function

To get the index of an Object in an Array in JavaScript, you can use the “Array.prototype.findIndex()” method. The Array.prototype.findIndex() is a built-in method that returns the index of the first element in the array that satisfies the provided testing function.

No element passed the test if the findIndex() method returns -1. Therefore, ECMAScript 2015 (ES6) introduced a new method called the findIndex() method to the Array.prototype. 

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 we s(the user assigns). 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 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.

Method 2: Using the “lodash” library

In JavaScript, the lodash is a lightweight library that 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 it finds, then it returns its index, which is 2, and if it does not see, it will return -1.

Leave a Comment