1

I need to fetch the keys and values of an Object in separate places and want to know if the order given in .keys() is consistent with that of .values()

I don't care about the order in relation to how they were inserted. Essentially, I just want to know that .keys()[0] corresponds to .values()[0], .keys()[1] corresponds to .values()[1], etc.

Do I need to convert it to a Map to ensure this parallel consistency or can I keep it as an Object?

superstar
  • 113
  • 3

1 Answers1

1

Both Object.keys() and Object.values() are specified the same way in the spec:

Object.values

  1. Let obj be ? ToObject(O).
  2. Let nameList be ? EnumerableOwnPropertyNames(obj, "value").
  3. Return CreateArrayFromList(nameList).

Object.keys

  1. Let obj be ? ToObject(O).
  2. Let nameList be ? EnumerableOwnPropertyNames(obj, "key").
  3. Return CreateArrayFromList(nameList).

EnumerableOwnPropertyNames specifies the order:

Order the elements of properties so they are in the same relative order as would be produced by the Iterator that would be returned if the EnumerateObjectProperties internal method were invoked with O.

So, yes, the order should be the same.

Mark
  • 74,559
  • 4
  • 81
  • 117