Open In App

Underscore.js _.findLastIndex() Function

Last Updated : 23 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

_.findLastIndex() function:
 

  • It is used to find the position of the element in an array starting from the end of the array.
  • We can even pass start the search from a mentioned index by mentioning it in the parameter.
  • If we want to find out the position of an element in an unknown array then we can use this function by passing only the array name and the element which need to be searched.

Syntax:
 

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

Parameters:
It takes three arguments:

  • The array
  • The predicate
  • The context (optional)

Return value:
It returns the position of the searched element in the array passed.
Examples:
 

  1. Passing a list of numbers to _.findLastIndex() function:
    The _.findLastIndex() function takes the element from the end of the list one by one and checks whether the number is same to the searching number given in the second parameter. Like here we are searching for the rollNo with value 1. In the list we have 2 rollNo with value 1. The _.findLastIndex() will take the first element containing rollNo 1 from the end of the list.
     

html




<!-- Write HTML code here -->
<html>
  
<head>
    <script src =
    </script>
</head>
  
<body>
    <script type="text/javascript">
        console.log(_.findLastIndex([{rollNo:1}, {rollNo:2},
        {rollNo:3}, {rollNo:1}], { rollNo : 1}));
    </script>
</body>
  
</html>


  1. Output:

  1.  
  2. Passing a structure to the _.findLastIndex() function:
    We can even pass characters with a lot of keys to the _.findLastIndex() function In this we only need to keep in mind which parameter/ key we want to use to find the last index. The key along with it’s value needs to be passed as the second parameter. The indexes start from 0 so the last element of the list will have an index 1 less than the size of the array. Like here, the parameter used to distinguish the elements of the list is taken as the key ‘name’.
     

html




<!-- Write HTML code here -->
<html>
  
<head>
    <script src =
    </script>
</head>
  
<body>
    <script type="text/javascript">
        var 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(_.findLastIndex(users, { name: 'Teddy'}));
    </script>
</body>
  
</html>


  1. Output:

  1. Passing same structure using another parameter:
    Here the same structure is passed as in the above example but the key taken for distinguishing the different parameters is different. Like in the second example we took name as the second parameter but here we will use the id key. Here the last index of the passed id ‘3’ is the last element of the list whose index is the size of the list minus one, which is, 5-1=4.
     

html




<!-- Write HTML code here -->
<html>
  
<head>
    <script src =
    </script>
</head>
  
<body>
    <script type="text/javascript">
        var 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(_.findLastIndex(users, { id : 3}));
    </script>
</body>
  
</html>


  1. Output:

  1.  
  2. Searching for an element that is not present in the list passed to the _.findLastIndex() function:
    In this we pass the structure same as in the above example and also the criteria for the search is same but the id passed is different, i.e., 100. This id as we can see is not present in the passed array to the _.findLastIndex() function. Therefore, in such a case the output will be a negative of one, -1.
     

html




<!-- Write HTML code here -->
<html>
  
<head>
    <script src =
    </script>
</head>
  
<body>
    <script type="text/javascript">
        var 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(_.findLastIndex(users, { id : 100}));
    </script>
</body>
  
</html>


  1. Output:

  1.  

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:
 

html




<!-- Write HTML code here -->
<script type="text/javascript"
</script>


An example is shown below:
 

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads