0

I have the following object:

Object {
  "1468": "Test Prop",
  "1647": "TEST 1",
  "1820": "ABC",
  "1840": "Gio"
}

I would like to order by the second property. It would be:

Object {
  "1820": "ABC",
  "1840": "Gio",
  "1647": "TEST 1",
  "1468": "Test Prop"
}

How can I do it in Javascript?

I received the data in an array:

Array [
  Array [
    1820,
    "ABC",
  ],
  Array [
    1840,
    "Gio",
  ],
  Array [
    1647,
    "Test 1",
  ],
  Array [
    1468,
    "Test Prop",
  ],
]

It is already ordered, but when I create my object I lost the order:

const myObj = {};
for (let index = 0; index < data.length; ++index) {
   const value = data[index];
   myObj[value[0]] = value[1];
}

Thanks

myTest532 myTest532
  • 2,091
  • 3
  • 35
  • 78
  • Property order in an object is defined by the language based on the order in which the properties were added over the object's lifetime. You can't control the ordering based on your own criteria. – Pointy Feb 14 '20 at 16:23
  • I guess you can remove and re-add them but even that ordering is implementation specific. – Jacob Feb 14 '20 at 16:24
  • I just included my array data and the object creation – myTest532 myTest532 Feb 14 '20 at 16:55

0 Answers0