There is usually no difference between iterating over an array's result in JavaScript, and its loop variable i is an array index starting with 0 (for in if the property collection of a non-array object is traversed, this I is the property name, or the key). Also note that the loop variable i is a string type, traversing the array with for. If you expand the array, then you need to pay attention to some problems when you iterate over the arrays with for.
Test code:
Copy Code code as follows:
Array.prototype.max = function ()
{
Return Math.max.apply ({}, this);
};
var company = [' Adobe ', ' Apple ', ' Google ', ' Intel ', ' Microsoft ', ' Oracle ', ' IBM ', ' SUN '];
alert (company.length); The length of the output here is 8, consistent with the length of the array defined above
The following Loop 9 times, in the first cycle of IE6, IE7 I value is not 0 but the above prototype extension method name Max, and in the IE8, FF browser last loop when the I value is max
for (var i in company)
{
Alert (' Index is: ' +i+ ' \nvalue is: ' +company[i]);
}
At this point, if you just want to print the list of 8 companies above, use the for In loop is not, even if you have to do so, but also in the loop to do some judgment, such as the following:
for (var i in company)
{
if (!isnan (i))
Alert (' Index is: ' +i+ ' \nvalue is: ' +company[i]);
}
Of course, in order to be safe, and not so lazy, to write a normal for loop is the most scientific, as follows:
for (var i=0; i< company.length; i++)
{
Alert (' Index is: ' +i+ ' \nvalue is: ' +company[i]);
}
This cyclic problem with an array of prototype extended attributes usually leads to unexpected results, but it can also give you difficult problems with the code, and look at the following example:
var userInfo = [["Tom", "020-12345678"],["Mike", 23, "020-87654321"]]; Obviously this is an array nesting
for (var i in UserInfo)
{
At this point through userinfo[i][0] You may not get the value you expected at all, for example, in this loop, you will see a result that has a value of undefined, because when I is Max, userinfo[i] is a function and not a child array like this [ "Tom", 21, "020-12345678"]
Alert (' Name: ' + userinfo[i][0] + ' \nage: ' + userinfo[i][1] + ' \nphone: ' + userinfo[i][2]);
}
In general, for-in is used to traverse object properties, and the array is to be traversed with a for (of course the array is also an object). It is understood that for more efficient traversal than for, and in addition to C #, ActionScript3.0 and foreach loops, this will be the most efficient loop, but JavaScript does not have this cycle. The most scientific array traversal should be like this: Use the normal for loop, and the length of the stored array. The code is as follows:
Copy Code code as follows:
var company = [' Adobe ', ' Apple ', ' Google ', ' Intel ', ' Microsoft ', ' Oracle ', ' IBM ', ' SUN '];
for (var i = 0, companynum = company.length i < companynum; i++)
{
Alert (' Index is: ' + i + ' \nvalue are: ' + company[i]);
}
Author: Webflash
Source: http://webflash.cnblogs.com