Javascript 在有效索引位置删除时,为什么拼接方法返回undefined?
为什么拼接方法返回undefined,并且不删除以下代码中位置4处的元素:Javascript 在有效索引位置删除时,为什么拼接方法返回undefined?,javascript,jquery,Javascript,Jquery,为什么拼接方法返回undefined,并且不删除以下代码中位置4处的元素: var excludedDepartmentsList = [1, 2, 3, 4, 5, 6]; var currentDepartmentId = 5; var position = $.inArray(currentDepartmentId, excludedDepartmentsList); if (position > -1) { var q = excludedDepartmentsList
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;
}
我在这里做了一个测试:
.splice
是一个函数,应该像excludedDepartmentsList.splice(位置,1)
一样调用它,而不是excludedDepartmentsList.splice[position,1]
。请注意,括号已从[]
更改为()
像下面这样使用()
,它应该返回5
excludedDepartmentsList.splice(position, 1)
固定小提琴:
[]
不等于()
你的语法有点错误。您可以为拼接功能键入
[]
,但它应该是()
[]
用于大多数编程语言上的数组,()
也用于函数/方法
您还必须返回q
if (position > -1) {
var q = excludedDepartmentsList.splice(position, 1);
return q;
}
拼接方法中的人工使用(),而不是[]
if (position > -1) {
var q = excludedDepartmentsList.splice(position, 1);
return q;
}