Underscore.js | invoke() with Examples

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
    <head>
        <title>_.contains() 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>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
    <head>
        <title>_.contains() 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>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
    <head>
        <title>_.contains() 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>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
    <head>
        <title>_.contains() 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>

chevron_right


Output:



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.