Open In App

Underscore.js _.findIndex() Function

Last Updated : 14 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Underscore.js _.findIndex() function is used to find the index of an element which is passed in the second parameter. We can use this for any kind of array like a number array, string array, character array, etc. If we do not know what elements are present in the array but we want to find out whether a single element is present or not then we use this function.

Syntax:

_.findIndex(array, predicate, [context])

Parameters:

  • array: The array
  • predicate: The value that needs to be found.
  • [context]: The context (optional)

Return value:

It returns the index at which the element to be searched is present.

Passing a list of only one key and its value to _.findIndex() function:

The ._findIndex() function takes the element from the list one by one and compares it with the element passed as the second parameter. If they match then it returns its index otherwise it just skips this element and goes on to the next. This process goes on till the match is not found or the list finishes. If the list finishes without finding the element passed then the result is -1.

Example: This example shows passing a list of only one key and its value to the _.findIndex() function.

HTML




<html>
 
<head>
    <script src =
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.findIndex([{rollNo:1}, {rollNo:2},
        {rollNo:3}], { rollNo : 1}));
    </script>
</body>
 
</html>


Output:

Passing a full structure to the _.findIndex() function:

We can even pass a structure with many properties to the _.findIndex() function and it will work in the same way. For this we also need to mention which property need to be compared. Like in the below example, the array has 3 properties, the is, name, last. Out of these we have mentioned that we want to compare and find out the index of the element with first name as ‘Teddy’.

Example: This example shows passing a full structure to the _.findIndex() function.

html




<!-- Write HTML code here -->
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        let users = [{ 'id': 1, 'name': 'Bobby', 'last': 'Stark' },
        { 'id': 2, 'name': 'Teddy', 'last': 'Lime' },
        { 'id': 3, 'name': 'Franky', 'last': 'Frail' },
        { 'id': 4, 'name': 'Teddy', 'last': 'Frail' }];
        console.log(_.findIndex(users, { name: 'Teddy' }));
    </script>
</body>
 
</html>


Output:

Comparing the property with number:

In this we have passed the same structure as the above but we have used the property to match and compare as ‘id’ which contains the numbers. It will work in the same way and compare all the ids until we get id as ‘3’ which is mentioned in the second parameter.

Example: This example shows comparing the property with number.

html




<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        let users = [{ 'id': 1, 'name': 'Bobby', 'last': 'Stark' },
        { 'id': 2, 'name': 'Teddy', 'last': 'Lime' },
        { 'id': 3, 'name': 'Franky', 'last': 'Frail' },
        { 'id': 4, 'name': 'Teddy', 'last': 'Frail' },
        { 'id': 3, 'name': 'Tinu', 'last': 'Thauus' }];
        console.log(_.findIndex(users, { id: 3 }));
    </script>
</body>
 
</html>


Output:

Passing an element in the second parameter which is not present in the list:

If we pass an element which the list does not contain then the result will be a negative number -1. There will be no errors. This is the case where the list ends but the element is not present in it.

Example: This example shows passing an element in the second parameter which is not present in the list.

html




<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        let users = [{ 'id': 1, 'name': 'Bobby', 'last': 'Stark' },
        { 'id': 2, 'name': 'Teddy', 'last': 'Lime' },
        { 'id': 3, 'name': 'Franky', 'last': 'Frail' },
        { 'id': 4, 'name': 'Teddy', 'last': 'Frail' },
        { 'id': 3, 'name': 'Tinu', 'last': 'Thauus' }];
        console.log(_.findIndex(users, { id: 100 }));
    </script>
</body>
 
</html>


Output:

NOTE: These commands will not work in Google console or in firefox as for these additional files need to be added which they didn’t have added.So, add the given links to your HTML file and then run them. The links are as follows: An example shown below:

<script type="text/javascript"
src ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>



Similar Reads

JavaScript Array findIndex() Function
JavaScript arr.findIndex() function is used to find the index of the first element that satisfies the condition checked by the argument function from the given array. Suppose you want to find the index of the first element of the array that is even. Syntax: array.findIndex(function(element, index, arr), thisValue) Parameters: The argument to this f
3 min read
Difference Between indexOf and findIndex function of array
The task is to differentiate between the indexOf() and findIndex() methods of JavaScript. we're going to discuss both approaches. JavaScript indexOf() Method: This method is used to find the index of the first occurrence of the elements provided for search as the argument to the function. Syntax: arr.indexOf(element[, index]) Parameters: element: T
2 min read
JavaScript typedArray.findIndex() with Example
The Javascript typedArray.findIndex() is an inbuilt function in JavaScript that is used to return an index in the tyedArray if the value satisfies the condition given in the function, otherwise, it returns -1. Syntax: typedarray.findIndex(callback) Parameters: It takes the parameter “callback” function which checks each element of the typedArray sa
2 min read
Lodash _.findIndex() Method
Loadsh _.findIndex() method is used to find the index of the first occurrence of the element. It is different from indexOf because it takes the predicate function that iterates through each element of the array. Syntax:_.findIndex(array, [predicate=_.identity], fromIndex);Parameters: array: It is the array in which the value is to be searched.predi
2 min read
What is the use of the Array.findIndex() method in JavaScript ?
The Array.findIndex() method in JavaScript is used to find the index of the first element in an array that satisfies a provided testing function. It returns the index of the first element for which the testing function returns true. If no such element is found, it returns -1. Syntax:array.findIndex(function(currentValue, index, arr), thisValue);Par
2 min read
TypeScript Array findIndex() Method
The findIndex() function, in TypeScript of array instances, helps you to find the index position of the element in an array that meets a condition, if no element meets the given condition it returns -1. Syntax:array.findIndex(callbackFn(value, index, array): boolean): numberParameters:callbackFn: A function that is called for each element in the ar
1 min read
JavaScript Array findIndex() Method
JavaScript Array findIndex() method iterates over array elements, returning the index of the first element passing a test. If no match, returns -1. Doesn't execute for empty elements, preserving the original array. Array findIndex() Syntaxarray.findIndex(function(currentValue, index, arr), thisValue);Array findIndex() Parametersfunction: It is the
4 min read
Underscore.js _.pluck Function
The Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects. The _.pluck() function is used when we need to extract a list of a given property. Like we have to find out the name of all the students, then we can
4 min read
Underscore.js _.unzip() Function
The Underscore.js is a JavaScript library that provides a lot of useful functions like the map, filter, invokes, etc even without using any built-in objects. The _.unzip() function is used to combine the same type of elements of different arrays into a single array. It matches one by one each passed array elements, to the next passed array elements
3 min read
Underscore.js _.chunk() Function
_.chunk() function: It is used to split a passed array into a number of arrays containing the number of elements given in the second parameter of the _.chunk() function.We can convert a single array into a number of arrays using this function.Also, the number of elements which the resultant array will contain should be given in the second parameter
3 min read