var myStringArray = ["Hello","World"];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
alert(myStringArray[i]);
//Do something
}
iterating arrays for-in
should be avoided, that statement is meant to enumerate object properties.It shouldn't be used for array-like objects because:
- The order of iteration is not guaranteed, the array indexes may not visited in the numeric order.
- Inherited properties are also enumerated.
Array.prototype
object to include a method there, that property will be also enumerated.For example:
Array.prototype.foo = "foo!"; var array = ['a', 'b', 'c']; for (var i in array) { alert(array[i]); }
The above code will alert, "a", "b", "c" and "foo!".That be particularly a problem if you use some library that relies heavily on native prototypes augmention (such as MooTools for example).
The
for-in
statement as I said before is there to enumerate object propertiesRead full article from Loop through array in JavaScript - Stack Overflow
No comments:
Post a Comment