How to remove the 0th indexed element in an array and return the rest of the elements in JavaScript?


The _.rest() is used to return the rest of the elements except the zeroth indexed element. It belongs to underscore.js, a library of javascript. It takes two parameters. One is an array and the other is an index. The second parameter is used to start the search from the given indexed array.

syntax

_.rest( array, index );

Example

In the following example, the 0th indexed element was removed using _.rest() method. Here no second parameter was passed.

Live Demo

<html>
<body>
<script 
   src ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
</script>
</head>
<body>
<script type="text/javascript">
   document.write(_.rest([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
</script>
</body>
</html>

Output

2, 3, 4, 5, 6, 7, 8, 9, 10


in the following example, the second parameter is also included. When a second parameter is provided, which is nothing but an index, those many numbers of elements will be removed and the rest of the elements will be displayed as shown in the output.

Example

Live Demo

<html>
<body>
<script 
   src ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
</script>
</head>
<body>
<script type="text/javascript">
   document.write(_.rest([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],4));
</script>
</body>
</html>

Output

5, 6, 7, 8, 9, 10
raja
Published on 10-Jul-2019 11:26:36
Advertisements