Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptUnderscore.js _.findIndex() Function

Underscore.js _.findIndex() Function

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




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

    
    

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




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

    
    

    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.




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

    
    

    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.




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

    
    

    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" 
</script>


An example is shown below:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments