Underscore.js | each() function

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 _.each() function is an inbuilt function in Underscore.js library of JavaScript which is used to return the each element of the given list.

Syntax:

_.each(list, function)

Parameters: It accepts two parameters which are specified below:

  • list: It is the list which contains some elements.
  • function: It is the function which is executed by taking each element of the list.

Return values: It returns each element of the list.



JavaScript code to show the working of this function:

  • Passing a list of numbers with alert() function: When the list of elements is passed to the alert inbuilt function then it takes the element from the list one by one and then shows it on the page (which is the function of the alert function). After alerting all the elements, the alert function ends and the _.each function also ends.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
      
    <head>
        <script>
                    underscore.js/1.9.1/underscore-min.js" >
        </script>
        <script type="text/javascript" 
             /1.9.1/underscore-min.js.map"></script>
      
        <script type="text/javascript" 
              /1.9.1/underscore.js"></script>
    </head>
      
    <body>
        <script type="text/javascript">
            _.each([1, 2, 3], alert);
        </script>
    </body>
      
    </html>

    chevron_right

    
    

    Output:

  • Passing a list of words with prompt() function: When the list of words is passed to the prompt inbuilt function then it takes the words from the list one by one and then shows it on the page along with its index fron the list (which is the function of the prompt function). After alerting all the elements, the prompt function ends and the _.each function also ends.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
      
    <head>
        <script>
                    underscore.js/1.9.1/underscore-min.js" >
        </script>
        <script type="text/javascript" 
             /1.9.1/underscore-min.js.map"></script>
      
        <script type="text/javascript" 
              /1.9.1/underscore.js"></script>
    </head>
      
    <body>
        <script type="text/javascript">
            _.each(["Geeks", "for", "Geeks"], prompt);
        </script>
    </body>
      
    </html>

    chevron_right

    
    

    Output:

  • Passing a list of numbers with a user defined function: First we need to create a function by using the ‘function’ keyword. Then, when we pass the list of elements to the user defined function then it takes the element from the list one by one and print it. After all the elements are passed, the function ends. And the _.each function also ends.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
      
    <head>
        <script>
                    underscore.js/1.9.1/underscore-min.js" >
        </script>
        <script type="text/javascript" 
             /1.9.1/underscore-min.js.map"></script>
      
        <script type="text/javascript" 
              /1.9.1/underscore.js"></script>
    </head>
      
    <body>
        <script type="text/javascript">
            function func() { document.write("Hi Geeks" +"<br>");}
            _.each({one: 1, two: 2, three: 3}, func);
        </script>
    </body>
      
    </html>

    chevron_right

    
    

    Output:

    Hi Geeks
    Hi Geeks
    Hi Geeks


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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.