This article describes how to use the key type in javascript for in. for more information, see.
Many while, for (var k = 0; k <10; k ++) {}, for in .., for example, the array is arr [154,256,369,852,952]. I want to calculate the average value of this array. I used to use less for in. I tried to use the for in loop this time, but it is still not the average value, the Code is as follows:
The Code is as follows: |
Copy code |
Function getAvgSum (arr, param, flag ){ Var as = 0; If (param ){ For (var ai in arr ){ As + = parseInt (arr [ai] [param]); } } Else { For (var ai in arr ){ // Alert (typeof ai ); As + = parseInt (arr [ai]); } } If (flag = 'avg '){ Return as/(ai + 1 ); // Return as/(parseInt (ai) + 1 ); } Return parseInt (); } |
After such alert debugging, I finally found the cause: the result of alert (typeof ai) is string, which is always thought to be a number. Do not feel the same
Differences between for in and for each in js
Both functions are used to traverse objects, but why do we need the for each in statement when we have the for in statement? Later, we can see the document for each in development, for each in is released in javascript 1.6 as part of the E4X standard and is not part of the ECMAScript Standard
The Code is as follows: |
Copy code |
Copytext Var Chao Mao = { Height: 185 ", Weight: "70 kg ", Age: 40 }; For (var I in ){ Document. write ("I =" + I + ".................. chao Mao [I] = "+ Chao Mao [I] +" <br> "); } /* Result I = height ................................ exceed Mao [I] = 185 I = weight ................................. exceed hair [I] = 70 kg I = Age */ For each (var I in ){ Document. write ("I =" + I + ".................. chao Mao [I] = "+ Chao Mao [I] +" <br> "); } /* Result I = 185CM .............................. Chao Mao [I] = undefined I = 70 kg ...... super hair [I] = undefined I = 40 .............................. Chao Mao [I] = undefined */Var superhair = { Height: 185 ", Weight: "70 kg ", Age: 40 }; For (var I in ){ Document. write ("I =" + I + ".................. chao Mao [I] = "+ Chao Mao [I] +" <br> "); } /* Result I = height ................................ exceed Mao [I] = 185 I = weight ................................. exceed hair [I] = 70 kg I = Age */ For each (var I in ){ Document. write ("I =" + I + ".................. chao Mao [I] = "+ Chao Mao [I] +" <br> "); } /* Result I = 185CM .............................. Chao Mao [I] = undefined I = 70 kg ...... super hair [I] = undefined I = 40 .............................. Chao Mao [I] = undefined
|
*/Note that the value of variable I is different. for each in cannot obtain the attribute name of the object. Only the attribute value can be obtained.
If you want to traverse objects, we recommend that you use for in
1. for in is more powerful than for each in traversing objects. for in, not only can all attribute names and attribute values be traversed, but for each in can only traverse attribute values.
2. for in is the syntax that comes out of javascript 1.0, and for each in is the syntax that comes out of javascript 1.6. Many Browsers Do not support u, such as IE6, 7, and 8, therefore, we recommend that you use for in.