Open In App

Underscore.js _.invokes Function

Improve
Improve
Like Article
Like
Save
Share
Report

The Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. 

The _.invoke() function is used to perform certain actions like sorting, joining, joining with some symbol, make upper case etc the elements of an array. It call the function directly by its name in it’s argument list. The mentioned function will be applied on all the array elements.

Syntax:  

_.invoke( list, methodName, *arguments ) 

Parameters:This function accepts three parameters as mentioned above and described below: 

  • List: This parameter is used to hold the list of data.
  • MethodName: This parameter is used to hold the test condition.
  • Arguments: This parameter needs to add some symbols between elements.

Return values: This function returns the list formed after the given function is applied on it. 

Passing the sort() function to the _.invoke() function: The ._invoke() function takes the element from the list one by one and do the specified function on the elements. The sort function will sort the list in ascending order. The output will contain a list of all the sorted lists.

Example: 

HTML




<html>
    <head>
        <title>_.invoke() function</title>
        <script type="text/javascript" src=
        </script>
        <script type="text/javascript" src=
        </script>
    </head>       
    <body>
        <script type="text/javascript">
              console.log(_.invoke([[1, 6, 8], 
                                    [7, 5, 3], 
                                    [33, 76, 55]], 
                                    'sort'));
        </script>
    </body>
</html>


Output: 

Passing the join() function to _.invoke() function: Pass the list along with the method name which has ‘join’. Then the elements of the list will be concatenated together. Any number of elements can be passed in the list.

Example:  

HTML




<html>
    <head>
        <title>_.invoke() function</title>
        <script type="text/javascript" src=
        </script>
        <script type="text/javascript" src=
        </script>
    </head>       
    <body>
        <script type="text/javascript">
             console.log(_.invoke([[11, 6, 8], [33, 25, 32]], 'join'));
        </script>
    </body>
</html>


Output: 

Passing toUpperCase() function to the _.invoke() function: The function toUpperCase() is only applied to the characters or strings not numbers (for obvious reasons that numbers are not in the lower case or the upper case). It need to pass the array of strings and the toUpperCase function to the _.invoke() function. The strings will be displayed in the upper case in the output.

Example:  

HTML




<html>
    <head>
        <title>_.invoke() function</title>
        <script type="text/javascript" src=
        </script>
        <script type="text/javascript" src=
        </script>
    </head>       
    <body>
        <script type="text/javascript">
             console.log(_.invoke(['geeks', 'for', 'geeks'], 'toUpperCase'));
        </script>
    </body>
</html>


Output:

Manipulating array by joining them with symbols: Join the elements of array with different symbols, characters etc. Join each element by using the same join() function. In this case, need to give three arguments, the list name, the join() function name and the symbol/character which need to join. The symbol will be inserted between every two elements.

Example:  

HTML




<html>
    <head>
        <title>_.invoke() function</title>
        <script type="text/javascript" src=
        </script>
        <script type="text/javascript" src=
        </script>
    </head>       
    <body>
        <script type="text/javascript">
             var manyThings = [ 
                    ['geeks', 'for', 'geeks'],
                    ['C++', 'C', 'java'],
                    ['HTML', 'CSS', 'JS'] 
             ];
             console.log(_.invoke(manyThings, 'join', ' # '));
        </script>
    </body>
</html>


Output: 

 



Last Updated : 24 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads