Get the Index of Object in an Array in JavaScript15 Feb 2025 | 4 min read We will understand how to get the index of an object in an array in JavaScript in this article. There are various methods that we can use to get the index of the object in the array which are as follows:
Let us understand each method one by one. Utilizing the map() methodThe map() method is utilized to construct a new array and calls a function for every element of the given array. It invokes the specified function only at one time for each array element. Syntax:In the above syntax, the following are the parameters discussed below: (function(current_value, index, arr): It defines the function that is to be executed for every array element. current_value: It defines the current value of the element. index: It defines the index of the array of the current element. arr: It defines an object in an array to which the current element belongs. thisValue: It defines the value which is to be passed through the function so as to utilize as its "this" value. The parameter "(function(current_value, index, arr)" and "current_value" are required. The parameter "index" and "arr" are optional. The parameter "thisValue" is set to undefined when this parameter does not consists of any value. Example:We will get the index of the object array utilizing the map() method in the following demo. Code: Output: We can witness that we have got the index value of the array object. ![]() Utilizing the findIndex() methodThe findIndex() method is utilized to run a function for each element of the array. It returns the first index of the array element. If the data is not found then it will return the value "-1". Example:We will get the index of the object array utilizing the findIndex() method in the following demo. Code: Output: Here is the output where we can witness that the index of array object is found. ![]() Utilizing the some() methodThe some() method is utilized to check if any of the elements available in the array fulfills the given condition then gives the value of the index. It executes a function for each element available in the array. Example:We will get the index of the object array with the help of the some() method in the following demo. Code: Output: Here is the output in which we can witness that the index of array object is found. ![]() Utilizing the for loopThe "for" loop is utilized to iterate over the array objects and check whether the given value is available in the array or not. Example:We will get the index of object array with the help of "for" loop in the following demo. Code: Output: Here is the output in which we can have a look at the index of the array object. ![]() Conclusion:We have understood how to get the index of the object in an array in JavaScript in this article. We have understood various methods with the help of examples such as map() method, findIndex() method, some() method and for loop. Next TopicHigher-order-functions-in-javascript |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India



