Underscore.js | _.findLastIndex()

_.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 _.lastIndexOf() function:
    The ._lastIndexOf() 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 _.lastIndexOf() will take the first element containing rollNo 1 from the end of the list.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- 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>

    chevron_right

    
    

    Output:

  2. Passing a structure to the _.lastIndexOf() function:
    We can even pass characters with a lot of keys to the _.lastIndexOf) 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’.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- 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>

    chevron_right

    
    

    Output:

  3. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- 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>

    chevron_right

    
    

    Output:

  4. Searching for an element that is not present in the list passed to the _.lastIndexOf() function:
    In this we pas 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 _.lastIndexOf() function. Therefore, in such a case the output will be a negative of one, -1.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- 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>

    chevron_right

    
    

    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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


An example is shown below:



My Personal Notes arrow_drop_up

I like to do coding in C++C and java programming languages HTML and CSS always intersts me Sharing knowleged is the best way according to me to increase ones knwoledge

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.