
Use lodash Library to Find Index of Object in JavaScript Array.Use findIndex() Method to Find the Index of the Object in an Array in JavaScript.Example 1įollowing example demonstrates the usage of findIndex() method in JavaScript.

If the element in the given array exists and satisfies the condition then, the index of element is returned or else ‘-1’ is returned. ThisArg − This is an optional parameter which is used as the value of “this” if passed and takes as “undefined” if it is not passed. Index − This is the optional parameter which has the access to index of the current element in the array.Īrray − This is an optional parameter which has the value of the given array object having the current element. They are −įunction − The function is used to execute on all the elements of the array until the constraint is satisfied and then returned true.Įlement − This is the current element in the array which undergoes the given functionality or the constraint in the given array.

The method ‘findIndex()’ will take five parameters including the function. The syntax given above is called as inline callback function syntax as the function and also the other parameters are called in one line at once. The syntax of the findIndex() method is − array.findIndex(function(element, index, array), thisArg) If no element is present with the given constraint or condition then, it returns -1. This method will return the index of the first element in the given array even if there are two or more numbers. The ‘findIndex()’ method in JavaScript arrays will return the first element from the given array with the given constraint being satisfied. In this tutorial a functionality of array object ‘findIndex()’ is explained and demonstrated with some examples. In JavaScript, Arrays are objects and these objects have some inbuilt functions and properties by which one can do the operations faster and easier.

For example, if array is declared as integer data type then it stores one or more elements of the integer data type. Array is a data type which can store multiple elements of similar data types.
