var i;
for (i = 0; i < fruits.length; i += 1) {
if (fruits[i] == "pear") {
fruits.splice(i, 1);
i -= 1;
}
}
6.4. Enumeration
Since JavaScript's arrays are really objects, the for in
statement can be used to iterate over all of the properties of an array.
Unfortunately, for in
makes no guarantee about the order of the properties, and most
array applications expect the elements to be produced in numerical order. Also, there is still the problem with
unexpected properties being dredged up from the prototype chain.
Fortunately, the conventional for statement avoids these problems. JavaScript's for statement is similar to
that in most C-like languages. It is controlled by three clauses: the first initializes the loop, the second is the while condition, and the third does the increment:
var i;
for (i = 0; i < myArray.length; i += 1) {
document.writeln(myArray[i]);
}
f
is not even an index (number)! – Derek 朕會功夫 Jun 15 '12 at 21:28fruit = fruit.filter(function(f) { return f !== "pear"; });
– squint Jun 15 '12 at 21:42