我想用一个函数从一个包含多个重复元素的数组中删除一个元素。
var array=["hello","hello","world",1,"world"];
function removeItem(item){
for(i in array){
if(array[i]==item) array.splice(i,1);
}
}
removeItem("world");
//Return hello,hello,1
removeItem("hello");
//Return hello,world,1,world
这个循环在连续重复两次时不会移除其中一个元素,只会移除其中一个。
为什么?