For (var index in MyArray) {//don ' t actually does this
Console.log (Myarray[index]);
}
Disadvantages:
- The index value of the array index is the string type "0", "1", "2", and so on, not the number type. When you perform arithmetic operations ("2" +1== "21") may not be the result you expect, so it is inconvenient to convert the type before the operation.
- The loop body not only iterates through the elements of the array, but even the expando properties. For example, if you have a property called name in your myarray array, the index = = "Name" is also traversed when traversing, so that it executes one more time. Immediately these properties are directly accessible on the prototype chain of the array.
- The most silent is that, in some cases, this code is arbitrary when iterating over the elements of an array.
Generally use the following two kinds:
Myarray.foreach (function (value) {
Console.log (value);
});
for (var index = 0; index < myarray.length; index++) {
Console.log (Myarray[index]);
}
Error methods for JS traversal array