Skip to content
Related Articles

Related Articles

Undercore.js | _.lastIndexOf()
  • Last Updated : 03 May, 2019

_.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.




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

    
    

    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.




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

    
    

    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.




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

    
    

    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.




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

    
    

    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:




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


An example is shown below:

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :