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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.