I do not understand the arrangement when inserting objects in another object. In this example:
const list = {
"mainVal": {
}
}
list['mainVal']['010'] = {"a" : "1"}
list['mainVal']['020'] = {"a" : "1"}
list['mainVal']['021'] = {"a" : "1"}
list['mainVal']['023'] = {"a" : "1"}
list['mainVal']['031'] = {"a" : "1"}
list['mainVal']['032'] = {"a" : "1"}
list['mainVal']['300'] = {"a" : "1"}
Results to:
{
"mainVal": {
"300": {
"a": "1"
},
"010": {
"a": "1"
},
"020": {
"a": "1"
},
"021": {
"a": "1"
},
"023": {
"a": "1"
},
"031": {
"a": "1"
},
"032": {
"a": "1"
}
}
}
How come 300
key is on the top of the object now when it has been 'inserted' last? So it is not in chronological order nor alphabetical order.
I checked related questions:
Does JavaScript Guarantee Object Property Order?
Does ES6 introduce a well-defined order of enumeration for object properties?
But both of them talk about arrangement of properties. I am specifically asking about arrangement of keys. And they say that it is either chronological or alphabetical, but this isnt the case here.
How can make it be in alphabetical order (or if that is not possible then can I force it to be in chronological order?):
{
"mainVal": {
"010": {
"a": "1"
},
"020": {
"a": "1"
},
"021": {
"a": "1"
},
"023": {
"a": "1"
},
"031": {
"a": "1"
},
"032": {
"a": "1"
},
"300": {
"a": "1"
}
}
}
Can anyone please shed some light? Thanks