Open In App

Underscore.js _.findIndex() Function

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>



Last Updated : 14 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads