var myarray = ["item 1", "item 2", "item 3", "item 4"];_x000D_
_x000D_
//removes the first element of the array, and returns that element._x000D_
alert(myarray.shift());_x000D_
//alerts "item 1"_x000D_
_x000D_
//removes the last element of the array, and returns that element._x000D_
alert(myarray.pop());_x000D_
//alerts "item 4"
_x000D_
"item 2", "item 3", "item 4"
when i remove the first elementThis question is related to
javascript
jquery
arrays
This should remove the first element, and then you can return the remaining:
var myarray = ["item 1", "item 2", "item 3", "item 4"];_x000D_
_x000D_
myarray.shift();_x000D_
alert(myarray);
_x000D_
As others have suggested, you could also use slice(1);
var myarray = ["item 1", "item 2", "item 3", "item 4"];_x000D_
_x000D_
alert(myarray.slice(1));
_x000D_
Why not use ES6?
var myarray = ["item 1", "item 2", "item 3", "item 4"];_x000D_
const [, ...rest] = myarray;_x000D_
console.log(rest)
_x000D_
Try this
var myarray = ["item 1", "item 2", "item 3", "item 4"];
//removes the first element of the array, and returns that element apart from item 1.
myarray.shift();
console.log(myarray);
This can be done in one line with lodash _.tail
:
var arr = ["item 1", "item 2", "item 3", "item 4"];_x000D_
console.log(_.tail(arr));
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
_x000D_
You can use array.slice(0,1) // First index is removed and array is returned.
Source: Stackoverflow.com