简体   繁体   English

只有一个参数的拼接不会删除整个数组

[英]splice with only one parameter not deleting the whole array

 a = ['a','b','c','d','e']; console.log(a.splice(1)); console.log(a);

In splice documentation, it says that skipping the delete parameter will delete all array items, yet here they're not;在 splice 文档中,它说跳过 delete 参数将删除所有数组项,但在这里它们不是; the first one is left out.第一个被排除在外。 Why is this?为什么是这样?

Edit: My expectations was that haveing the omission of the delete parameter would cause to delete all the items of the array.编辑:我的期望是省略 delete 参数会导致删除数组的所有项目。 But now after read Baconnier comment I understand that removes all after the index (first parameter).但是现在在阅读 Baconnier 评论后,我明白在索引(第一个参数)之后删除了所有内容。

then all the elements from start to the end of the array will be deleted.那么数组从头到尾的所有元素都将被删除。 start being 1 in your case.在您的情况下开始为 1。

a = ['a','b','c','d','e'];
console.log(a.splice(0));
console.log(a);

Arrays start at 0, if you want to delete all elements including the first one you need to pass 0 as the first parameter. Arrays 从 0 开始,如果要删除包括第一个元素在内的所有元素,则需要将 0 作为第一个参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM