An explanation of the sequence of JS array objects

Source: Internet
Author: User

First, the JS object traversal output is really in order to output it?

Here's how to practice:

var obj={' 3 ': ' CCC ', Name: ' abc ', Age:23,school: ' Sdfds ', class: ' DFD ', hobby: ' DSFD '};object.keys (obj) 

Output: ["3", "Name", "Age", "School", "class", "hobby");

Change the order of the objects,

var obj={name: ' abc ', ' 3 ': ' CCC ', Age:23,school: ' Sdfds ', class: ' DFD ', hobby: ' DSFD '};object.keys (obj) 

Output: ["3", "Name", "Age", "School", "class", "hobby")

So, the object's traversal output is not in order, then according to what rules, and carefully in-depth study you will find that it is also related to the browser, chrome and IE is not the same, so give the following conclusions:

The JavaScript parsing engine for Chrome Opera follows the new ECMA-262 version fifth specification. Therefore, traversing the book order when traversing object properties with the For-in statement is not a property-building sequence.
While IE6 IE7 IE8 Firefox Safari's JavaScript parsing engine follows the older ECMA-262 Third Edition specification, the Order of attribute traversal is determined by the order in which the property is constructed.

Using the For-in statement to traverse object properties in Chrome Opera follows a pattern:
They first extract all of the key's parsefloat values as non-negative integers, then sort the attributes in numerical order first, then iterate through all the remaining properties in the order defined by the object.

So, what's the solution?

The object can be extracted by object.keys the key out of the object into an array, and then through the array of sorting, and then iterate through the array, through the key to fetch the object's property values.

second, JS array traversal sorting method

 We all know that the array traversal is in order, so when a lot of objects are placed in an array, what do you need to do to sort the array according to one of the properties of the object?

1. Define a method:
varCompare =function(prop) {return function(Obj1, obj2) {varVal1 =Obj1[prop]; varVal2 = Obj2[prop];if(Val1 <val2) {            return-1; } Else if(Val1 >val2) {            return1; } Else {            return0; }                } }
var arr=[{name: "Wlz", age:25},{Name: "Wlz", age:5},{Name: "Wlz", age:15},{Name: "Wlz", age:3},{Name: "ZLW", age :}];console.log (Arr.sort (Compare (' age)))

The last array is sorted according to the attributes.

An explanation of the sequence of JS array objects

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.