Underscore.js | _.findIndex()

_.findIndex() function:

  • It 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 number array, string array, character array etc.
  • If we do not know what all 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:
It takes three arguments:



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

Return value:
It returns the index at which the element to be searched is present.

Examples:

  1. Passing a list of only one key and it’s 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 it’s 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.
    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(_.findIndex([{rollNo:1}, {rollNo:2},
            {rollNo:3}], { rollNo : 1}));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  2. 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’.
    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(_.findIndex(users, { name: 'Teddy'}));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  3. 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.
    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(_.findIndex(users, { id : 3}));
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  4. 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.
    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(_.findIndex(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.