Javascript 从数组中删除第一个元素并返回减去第一个元素的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38096687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:05:39 来源:igfitidea点击:
remove first element from array and return the array minus the first element
提问by Brownman Revival
var myarray = ["item 1", "item 2", "item 3", "item 4"];
//removes the first element of the array, and returns that element.
alert(myarray.shift());
//alerts "item 1"
//removes the last element of the array, and returns that element.
alert(myarray.pop());
//alerts "item 4"
- How to remove the first array but return the array minus the first element
- In my example i should get
"item 2", "item 3", "item 4"
when i remove the first element
- 如何删除第一个数组但返回减去第一个元素的数组
- 在我的例子中,
"item 2", "item 3", "item 4"
当我删除第一个元素时我应该得到
回答by Jesper H?jer
This should remove the first element, and then you can return the remaining:
这应该删除第一个元素,然后您可以返回剩余的元素:
var myarray = ["item 1", "item 2", "item 3", "item 4"];
myarray.shift();
alert(myarray);
As others have suggested, you could also use slice(1);
正如其他人所建议的,您也可以使用 slice(1);
var myarray = ["item 1", "item 2", "item 3", "item 4"];
alert(myarray.slice(1));
回答by Tudor Morar
Why not use ES6?
为什么不使用 ES6?
var myarray = ["item 1", "item 2", "item 3", "item 4"];
const [, ...rest] = myarray;
console.log(rest)
回答by I'm Geeker
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);
回答by Penny Liu
回答by Hassan Abbas
You can use array.slice(0,1) // First index is removed and array is returned.
您可以使用 array.slice(0,1) // 删除第一个索引并返回数组。