1

Is it safe to assume that calling Object.keys() on object always returns array in the same order? I mean it wasn't specified in JS until ES2015 but I'm not sure how it is exactly in React Native?

  • 1
    If your code relies on the order of object keys, you're going to have a bad time. – SpeedOfRound Sep 13 '19 at 19:22
  • No, object is not an array. Thus, you shouldn't use it if order of items matters to you. – Rachel Nicolas Sep 13 '19 at 19:39
  • Always in the same order for the same object, yes, but you should not expect it to return keys in a specific order. If you need that, better sort the output explicitly. – Bergi Oct 20 '19 at 20:23

1 Answers1

0

Property keys are traversed in the following order:

First, the keys that are integer indices in ascending numeric order.

Then, all other string keys, in the order in which they were added to the object.

Lastly, all symbol keys, in the order in which they were added to the object.

Many engines treat integer indices specially (even though they are still strings, at least as far as the ES6 spec is concerned). Therefore, it makes sense to treat them as a separate category of keys.