The order of object property enumeration is not alphabetical.
Implementations are left to decide the order, as discussed here.
Most browsers tend to follow the order in which properties were created.
If you require specific ordering, consider using an Array
, Map
, or Set
.
With ES2015, you have the option to make an object iterable by defining a custom iteration function that can specify the order of enumeration needed.
var obj = {
foo: '1',
bar: '2',
bam: '3',
bat: '4',
};
obj[Symbol.iterator] = iter.bind(null, obj);
function* iter(o) {
var keys = Object.keys(o);
for (var i=0; i<keys.length; i++) {
yield o[keys[i]];
}
}
for(var v of obj) { console.log(v); } // '1', '2', '3', '4'