According to my research, the order of the keys in the for..in loop should be undefined / unreliable
Undefined, yes.
- but if left undisturbed, it should be in the input order
No, you were right the first time: It is undefined. Even in ES2015 (hereinafter referred to as βES6β) and higher, which provide a property order for some other operations, the older for-in
and Object.keys
do not need to follow the order defined for the new ones.
In these other operations ( Object.getOwnPropertyNames
, JSON.serialize
, ...), the order ( indicated here ) is not purely the insertion order: properties whose names are array indexes according to the specification specification *, first, in numerical order. Most of the core JavaScript engines have updated the for-in
processing according to their handling of these new operations (many have already processed the indexes of arrays in different ways, but they varied depending on whether they were placed before the indices without an array or after), but again, it is undefined and you should not rely on it.
If you need a clean insertion order, ES2015 Map
provides that regardless of key value. There are no objects.
Here's an example using Map
:
const map = new Map([ ['2', { name: 'bus', price: 10 }], ['3', { name: 'foot', price: 0 }], ['1', { name: 'taxi', price: 100 }] ]); for (const entry of map.values()) {
* Specification specification for array index:
An integer index is the key of the String-value property, which is a canonical numeric string (see 7.1.16) and whose numeric value is +0 or a positive integer β€ 2 53-1 . The array index is an integer index whose numerical value I is in the range +0 β€ i <2 32-1 .
source share