Description of key types in for in javascript

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.