为什么下面代码中的 splice 方法返回 undefined 并且没有删除位置 4 的元素:
var excludedDepartmentsList = [1, 2, 3, 4, 5, 6];
var currentDepartmentId = 5;
var position = $.inArray(currentDepartmentId, excludedDepartmentsList);
if (position > -1) {
var q = excludedDepartmentsList.splice[position, 1];
return;
}
我在这里做了一个测试:http://jsfiddle.net/PnVEb/
最佳答案
.splice
是一个函数,应该像 excludedDepartmentsList.splice(position, 1)
那样调用,而不是 excludedDepartmentsList.splice[position, 1]
。请注意,括号从 []
更改为 ()
。
像下面一样使用()
,它应该返回5
excludedDepartmentsList.splice(position, 1)
固定 fiddle : http://jsfiddle.net/PnVEb/1/
关于javascript - 为什么在有效索引位置删除时 splice 方法返回 undefined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16989293/