![](/img/trans.png)
[英]Why is my splice deleting all but the first instead of only the one specified?
[英]splice with only one parameter not deleting the whole array
a = ['a','b','c','d','e']; console.log(a.splice(1)); console.log(a);
在 splice 文档中,它说跳过 delete 参数将删除所有数组项,但在这里它们不是; 第一个被排除在外。 为什么是这样?
编辑:我的期望是省略 delete 参数会导致删除数组的所有项目。 但是现在在阅读 Baconnier 评论后,我明白在索引(第一个参数)之后删除了所有内容。
那么数组从头到尾的所有元素都将被删除。 在您的情况下开始为 1。
a = ['a','b','c','d','e'];
console.log(a.splice(0));
console.log(a);
Arrays 从 0 开始,如果要删除包括第一个元素在内的所有元素,则需要将 0 作为第一个参数传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.