What's the Order of Array Returned by JavaScript Object.entries()?

Prior to ES11, Object.entries() did not guarantee the order of the resulting array. Different browsers handled it differently. However, from ES11+, the Object.entries() method has the same ordering as the for...in loop. This means that the order of the object (own, enumerable) properties when looping, and the order of object entries array, would be the following:

  1. Numeric keys in ascending order;
  2. String keys in insertion order;
  3. Symbols in insertion order.

For example:

const sym = Symbol('a');
const obj = { foo: 'bar', 123: 'baz', 0: 'qux', [sym]: 'quux' };

console.log(Object.entries(obj)); // [['0', 'qux'], ['123', 'baz'], ['foo', 'bar']]

The resulting array from Object.entries() does not contain Symbol property/value because Symbol is non-enumerable and Object.entries() only returns an array of object's (own) enumerable properties.

In cases where there's a need for a certain type of ordering of object entries, you can simply sort the resulting array or use a Map() instead.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.