Do Object.keys() and Object.values() methods return arrays that preserve the same order

In short, Yes.

Both Object.keys and Object.values (and also Object.entries()) enumerate via for-in loop on the object.
Since both use the same [[Enumerate]], as long as they are called on an object with same unchanged own prototype (without deleting or adding keys between the keys and values calls), the order will be the same.

What will the order be?
It depends on how you initialized your object, which explorer you are using, and the order of which you have inserted additional keys to the object.
If the actual order is important to you as well (and not only that both keys and values result will be in to the same order) the bulletproof approach will be to use an array.

A different approach: Object.entries() will return a list of [key, value] pairs (in the same order provided by a for-in loop as well) which might be more helpful for your use case.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)