JavaScript for-in orderly traversal of JSON data and explore various browser differences _javascript tips

Source: Internet
Author: User

Object itself is a collection of objects that are not, so the order of the attributes that are traversed when you iterate through the properties of an object with the For-in statement differs from the object definition.

Understanding the standards of the consortium:

As described in the ECMA-262 (ECMAScript) Third Edition, the Order of the property traversal of the for-in statement is determined by the order in which the attributes are written in the object definition.

For more information on the for-in statement in the ECMA-262 (ECMAScript) Third edition, refer to the Edition 12.6.4 in ECMA-262 3rd for-in.

In the current ECMA-262 (ECMAScript) Fifth Edition specification, the traversal mechanism of the for-in statement is adjusted again, and the Order of attribute traversal is not stipulated.

For more information on the for-in statement in ECMA-262 (ECMAScript) version fifth, refer to the Edition 12.6.4 in ECMA-262 5rd for-in.

The attribute traversal order description in the new version differs from earlier versions, which results in inconsistencies in the traversal order of the attributes when the JavaScript parsing engine that adheres to the ECMA-262 Third edition of the specification implements the For-in statement and follows the parsing engine implemented by the fifth edition specification.

As a result, you should avoid writing code that relies on the order of object attributes in development. As follows:

<script>
var json1 = {
    "2": {"name": "1th"},
    "1": {"name": "2nd"},
    "3": {"name": "3rd"}
}
var json2 = [
    {' name ': ' 1th '},
    {' name ': ' 2nd '},
    {' name ': ' 3rd '}
] for
(var i in Json1) {
    alert (json1[i].name);
}
Correct for
(var i in Json2) {
    alert (json2[i].name);
}
</script>

Look at the for-in code in a browser difference:

The following is a piece of code to introduce the JS Loop through the JSON data alone

JSON data such as:

{"Options": "[{/" text/":/" Wangjiawan/",/" value/":/" 9/"},{/" text/":/" Li Jia Wan/",/" Valu
e/":/" 10/"},{/" text/":/" Shaojia Bay/",/" value/":/" 13/"}]"}

Can be written in JS:

var data=[{name: "A", Age:12},{name: "B", Age:11},{name: "C", Age:13},{name: "D", age:14}]; 
   for (var o in data) { 
    alert (o); 
    Alert (Data[o]); 
    Alert ("Text:" +data[o].name+ "value:" +data[o].age); 
   

Or

<script type= "Text/javascript" > 
function Text () { 
 var json = {"Options": "[{/" text/":/" Wangjiawan/",/" value/" :/"9/"},{/"text/":/"Li Jia Wan/",/"value/":/"10/"},{/"text/":/"Shaojia Bay/",/"value/":/"13/"} "}  
 JSON = eval (json.options) For 
 (var i=0; i<json.length; i++) 
 { 
   alert (json[i].text+ "" + Json[i].value) 
 } 
} 
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.