我想使用函數刪除數組中出現多次的元素。循環移除多次出現的數組中的元素
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
該循環時,它在序列重複兩次不會刪除該元素,只刪除其中之一。
爲什麼?
請問這項工作atall?變量我不會等於數組的索引,只是變量數組 –