Underscore.js | _.rest() with Examples

The Underscore.js is a JavaScript library that provides a lot of useful functions like the map, filter, invoke etc even without using any built-in objects.
The _.rest() is used to return the rest of the elements except the zeroth indexed element. Second parameter is used to start the search from the given indexed array. It is used to return all the array elements. When the element comes then only they are displayed except the first element of the index.

Syntax:

_.rest( array, [index] ) 

Parameters: This function accepts two parameter which are listed below:

  • array: This parameter is used to hold the array elements.
  • index: This parameter is used to hold the index number.

Return value: It returns the rest elements in an array.

Passing a number array to the _.rest() function: The ._rest() function takes the element from the list one by one and displays the elements directly. Then number element is given to the _.rest() function. It ignores the first element from the array and then displays all the others.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <script src
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.rest([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
        </script>
    </body>
</html>                    

chevron_right


Output:

Passing a word array to the _.rest() function: The ._rest() function takes the element from the list one by one and display the elements directly. It ignores whether the function is taking a numbered array or character array or any other. Then number element is given to the _.rest() function. It ignores the first element from the array and then displays all the others.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <script src
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.rest(['html', 'css', 'js', 
                        'ajax', 'php', 'node.js']));
        </script>
    </body>
</html>                    

chevron_right


Output:

Passing a special character array to the _.rest() function: The ._rest() function takes the element from the list one by one and displays the elements directly inspite of the fact that it is containing special characters. Then the character element is given to the _.rest() function. It ignores the first element from the array and then displays all the others.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <script src
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.rest(['!', '@', '#', '$', '%', '^']));
        </script>
    </body>
</html>                    

chevron_right


Output:

Passing the second parameter to the _.rest() function: The second parameter is used to start displaying the elements from that index. It is used to ignore more than one array element as one element is ignored by default. It works the same for the rest of the elements.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <script src
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.rest(['1', 'javascript', '#',
                            '2', 'underscore', '^'], 2));
        </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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script type="text/javascript" src =  
</script

chevron_right




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.