var fruit = ["apple","pear","pear","pear","banana"];
如何從此數組中刪除所有「pear」水果?
我嘗試以下,但還是一個梨仍然是:如何從javascript中的foreach循環中刪除特定的數組元素
for(var f in fruit) {
if (fruit[f] == "pear") {
fruit.splice(f, 1);
}
}
for(var f in fruit) {
document.write(fruit[f]+"<br>");
}
輸出:
apple
pear
banana
我在做什麼錯?現場演示:http://jsfiddle.net/SbxHc/
等待...'F'甚至不是一個索引(數字)! –
'fruit = fruit.filter(function(f){return f!==「pear」;});' – 2012-06-15 21:42:59