var fruit = ["apple","pear","pear","pear","banana"];
How do I remove all “pear” fruit from this array?
I tried the following, but still one pear remains:
for(var f in fruit) {
if ( fruit[f] == "pear" ) {
fruit.splice(f, 1);
}
}
for(var f in fruit) {
document.write(fruit[f]+"<br>");
}
Outputs:
apple
pear
banana
What am I doing wrong? Live demo: http://jsfiddle.net/SbxHc/
1 Answer