0
var arr = [3,5,6,7,8,9,0];
var obj = {};

for (var i = 0; i < arr.length; i++) {
    obj[arr[i]]    = {};
}

for (var key in obj) {
    console.log(key);  // 0, 3, 5, 6, 7, 8, 9
}

The value I thought it was.

3 5 6 7 8 9 0

How do I do this?

codejay
  • 21
  • 5
  • Use a Map or keep the array and use it to iterate over the keys, since the order will be preserved then. It's generally a bad idea to rely on the order of keys in an object. You've found one of the reasons why. The order is guaranteed, if you use non-index and non-Symbol keys, they'd be listed in insertion order. However, that can also easily be disturbed in the future. A classic example is serialising the object through JSON and deserialising it (e.g., when sending it through the network). That can re-arrange the keys. – VLAZ Jul 21 '21 at 05:17

0 Answers0