Chrome re-ordering object keys if numerics, is that normal/expected Chrome re-ordering object keys if numerics, is that normal/expected google-chrome google-chrome

Chrome re-ordering object keys if numerics, is that normal/expected


It's the way v8 handles associative arrays. A known issue Issue 164 but it follows the spec so is marked 'working as intended'. There isn't a required order for looping through associative arrays.

A simple workaround is to precede number values with letters e.g: 'size_7':['9149','9139'] etc.

The standard will change in the next ECMAScript spec forcing [chrome] developers to change this.


It would appear that Chrome is treating the integer string as if it were a numeric type when used as an index/property name.

I think relying on the Javascript implementation to preserve the order of what, in some cases, is object properties, and in other cases (certainly with chrome) array indices, is demonstrably an unsafe approach and order of enumeration is probably not defined in the spec. I would suggest adding an additional property to the JSON that indicates a sort order:

{    "7":{"sortOrder":1,"data":["9149","9139","10455","17208"]},    "7.5":{"sortOrder":2,"data":["9140","9150","10456","17209"]}    //etc}


When iterating over the properties of an object, the order is specified in the ECMAScript specification as being undefined and any order you may have observed in some environment should not be relied upon. If you need order, use an Array.


matomo