我有这段代码用于在 JS 中按值从数组中删除一个项目...
function remove_item(index){
//log out selected array
console.log('before >> ' + selected); //
//log out item that has been requested to be removed
console.log('removing >> ' + index);
//remove item from array
selected.splice( $.inArray(index,selected) ,1 );
//log out selected array (should be without the item that was removed
console.log('after >> ' + selected);
//reload graph
initialize();
}
这就是我的数组的样子......
selected = [9, 3, 6]
如果我打电话给remove_item(3)
这就是注销...
before >> 9,3,6
removing >> 3
after >> 9,3
之后应该9,6
不是9,3
我对此完全感到困惑,因为它有时有效,有时无效......
例如,我刚刚尝试过remove_item(10)
,这行得通...
before >> 1,2,10
removing >> 10
after >> 1,2
我确定它与这条线有关:
selected.splice( $.inArray(index,selected) ,1 );
非常感谢任何帮助。