Undercore.js | _.lastIndexOf()

_.lastIndexOf() function:

  • It finds the index of an element in an array.
  • If the element is repeated then it gives the index of that element which is closest to the end.
  • It is used when we have a number of repeated elements in the array and we want to find the index of that repeated element which is nearest to the end.

Syntax:

_.lastIndexOf(array, value, [fromIndex])

Parameters:
It takes three arguments:



  • The array
  • The value
  • The index from which search needs to start (optional)

Return value:
It returns the index of the repeated element which is closest to the end of the array.

Examples:

  1. Passing a list of characters to the _.lastIndexOf() function:
    The _.lastIndexOf() function takes the element from the list one by one starting from the end of the array and checks whether that element matches with the second parameter passed or not. If it matches, then the index of that element is returned otherwise that element is ignored and the element preceding it is checked in the passed array.
    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(_.lastIndexOf(['HTML', 'CSS', 'JS', 'AJAX',
                            'PEARL', 'CSS', 'HTML', 'CSS'], 'CSS'));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  2. Passing a value which is not present in the array:
    If we pass an element as the second parameter which is not present in the array then the _.lastIndexOf() function will work in the same manner. the search will start in the same manner for that element. It will keep on ignoring the present element and go onto the preceding element till the list ends. Once the array is exhausted then this function will return a negative value, -1 indicating that the element passed is not present in the array.
    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(_.lastIndexOf(['HTML', 'CSS', 'JS', 'AJAX', 
                           'PEARL', 'CSS', 'HTML', 'CSS'], 'GEEKS'));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  3. Passing a list of numbers to _.lastIndexOf() function:
    The _.lastIndexOf() function takes the element from the list one by one and works the same way as it does for characters in example 1. This function does not distinguish between a number or a character or string of characters. Therefore, the search will go on smoothly and the result will be displayed.
    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(_.lastIndexOf([1, 2, 3, 4, 5, 6], 4));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  4. Passing a heterogeneous array to the _.lastIndexOf() function:
    Like we can pass a number array, a character array etc in the same way we can also pass a heterogeneous array. A heterogeneous array contains the combination of numbers, strings or characters. The _.lastIndexOf() function will also not give any error in this case. Rather will give the expected output.
    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(_.lastIndexOf(['HTML', 1, 'CSS', 'JS', 2,
                     'AJAX', 'PEARL', 'CSS', 3, 'HTML', 'CSS'], 3));
        </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" src =
</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.