How To Get The Index Of An Object In An Array In JavaScript

Get the index of an Object in an Array in JavaScript

To get the index of an object in an array in JavaScript we have two methods: Using Array.findIndex() and Using indexOf(). Each way is rather straightforward, and you can choose whichever you like.

Get the index of an object in an array in JavaScript

Using Array.findIndex() 

This method will return the first element’s position in an array that has been returned true when passed to the checking function. Otherwise, this method returns -1 if the object is not in the array.

Syntax:

findIndex( (element) => { /* checking and return true or false*/ } )

First, we need an example to know exactly what the function will return:

var learn = {"learn":"LEARN"}
var share ={"share":"SHARE"}
var it={"it":"IT"}

// Create an array of objects
var array = [share, it, learn]

// Build a testing function that return true if equal the object {"share":"SHARE"}
let testing = (element) => element ==  share

// Get the index of the object “share” in the array
var index = array.findIndex(testing)
console.log(index)

Output: 

0

The example below declares that we have three objects, and then we push these objects into an array by random order. We then use the findIndex function to get the index of an object (in this case, the second object we created) in the array. However, in practice, we usually don’t get the index of an object stored in a variable. Instead, we often get the index of an object returned from a third method or function, such as find() in an array. For example: 

var learn = {"name":"LEARN"}
var share ={"name":"SHARE"}
var it={"name":"IT"}

// Create an array of objects
var array = [share, it, learn]

// Use find() to find an element whose name is "LEARN"
const object = array.find(element => element.name =="LEARN");

// Build a testing function that return true if equal the object we have founded
let testing = (element) => element ==  object

// Get the index of that object in the array
var index = array.findIndex(testing)
console.log(index)

Output:

2

Using indexOf()

The findIndex() method in the previous solution does not work in browser Internet Explorer. If you want to support this browser, you can consider using the indexOf() method:

Syntax:

array.indexOf(object)

Parameter:

  • object: the object you want to get its index in the array.

This function will return the index of an object which you have stored in an variable and passed it to this method:

var learn = {"name":"LEARN"}
var share ={"name":"SHARE"}
var it={"name":"IT"}

// Create an array of objects
var array = [share, it, learn]

// Use find() to find an element whose name is "LEARN"
const object = array.find(element => element.name =="LEARN");

// Get the index of that object in the array
var index = array.indexOf(object)
console.log(index)

Output: 

2

As you can see, this solution provides the same result as the first one. Each method is rather straightforward and helpful depending on what your context of the program is.

Summary

We have shown you how to get the index of an object in an array in JavaScript in some different methods. It would be helpful if you considered that the first approach is what most coders use. There is also a different tutorial that you can read in our page that is related to your problem, which is How To Find An Object In An Array In Typescript.

Maybe you are interested:

Leave a Reply

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