关闭。这个问题需要debugging details .它目前不接受答案。
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将帮助其他人回答问题。
5年前关闭。
Improve this question
我试图根据数组中的索引删除某些元素。
例子:
var x = ["a","b","c"];
并试图做
x.splice(x.indexOf("d"),1)
因为结果是
x.splice(-1,1)
- 它最终从数组中删除元素“c”!即使javascript中没有数组的循环属性,也无法理解为什么会发生这种情况。
最佳答案
来自 MDN page for splice
:
If negative, will begin that many elements from the end.
所以
x.splice(-1, 1)
从 x
末尾开始一个元素并删除单个元素。
关于javascript - 为什么 Array.splice(-1,1) 删除 Javascript 中的最后一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39877777/