Javascriptfor-in traversal of json Data Order differences between browsers

Source: Internet
Author: User
The object itself is a set of objects without objects. Therefore, when you use the for-in statement to traverse the attributes of an object, the order of the retrieved attributes is different from that of the object definition. Understanding W3C standards: As described in the third edition of ECMA-262 (ECMAScript), f

The object itself is a set of objects without objects. Therefore, when you use the for-in statement to traverse the attributes of an object, the order of the retrieved attributes is different from that of the object definition.
Understand W3C standards:

As described in the third edition of ECMA-262 (ECMAScript), the order of attribute traversal for a for-in statement isThe order in which attributes are written depends on the object definition..

For more information about The for-in Statement in The third Edition of ECMA-262, see 12.6.4 The for-in Statement in ECMA-262 3rd Edition.

In the latest ECMA-262 (ECMAScript) Fifth Edition specification, the for-in statement traversal mechanism has been adjusted,The order of attribute traversal is not specified..

For more information about The for-in Statement in ECMA-262 (ECMAScript) Fifth Edition, see 12.6.4 The for-in Statement in ECMA-262 5rd Edition.


The attribute traversal order in the new version is different from that in earlier versions, which will lead to JavaScript parsing engines implemented following the ECMA-262 third-edition specification when processing for-in statements, the traversal sequence of attributes is inconsistent with the parsing engine implemented in compliance with the fifth edition specification.

Therefore, we should try to avoid writing code that depends on the object attribute sequence during development. As follows:
Script var json1 = {"2": {"name": "1st"}, "1": {"name": "2nd "}, "3": {"name": "3rd"} var json2 = [{"name": "1st" },{ "name ": "2nd" },{ "name": "3rd"}] for (var I in json1) {alert (json1 [I]. name);} // correct for (var I in json2) {alert (json2 [I]. name);} script

Let's take a look at the differences between the for-in code in different browsers:

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.