Object.entries()

Returns an array of an object's own enumerable string-keyed [key, value] pairs.

Signature
Object.entries(obj)
  • Added in JavaScript ES2017

Parameters

ParameterTypeDescription
$objObjectAn object. Values that are not objects are coerced to objects first, except null and undefined.

Return value

Array<[string, any]>An array of the given object's own enumerable string-keyed property key-value pairs, each pair a two-element array of [key, value].

Errors

Throws a TypeError if obj is null or undefined, since neither can be coerced to an object.

Object.entries() turns an object into an array of [key, value] pairs, using the same own-and-enumerable filter as Object.keys(). It exists so you can use array methods on objects.

When to reach for it

Pair it with for...of and array destructuring for the cleanest object loop in the language:

for (const [key, value] of Object.entries(obj)) {
  // ...
}

Reach for it when you need both halves at once: filtering fields by their value, sorting an object by value, mapping keys and values into new shapes, or building a Map — new Map(Object.entries(obj)) works directly because a Map constructor takes exactly this pair format. Feed the result back through Object.fromEntries() to get an object again, which is the idiomatic way to filter or rename an object's fields.

What trips people up

Keys are always strings, so numeric keys arrive as '2', not 2, and comparisons like key === 2 silently fail. The nested arrays are fresh, but the values inside them are the same references as in the source object. Integer-like keys are still enumerated first, in ascending numeric order.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Loop an object with for...of and destructuring instead of for...in plus a hasOwnProperty guard
  • Sort an object's fields by value to build a leaderboard
  • Filter an object down to the fields that pass a test, then rebuild it with Object.fromEntries()
  • Build a Map from a plain configuration object

Frequently asked questions

How do I loop over an object's keys and values at the same time?

Use for (const [key, value] of Object.entries(obj)) — it destructures each pair and, unlike for...in, never visits inherited properties.

What is the difference between Object.entries() and for...in?

Object.entries() returns only own enumerable properties as an array, while for...in also walks the prototype chain, which is why for...in usually needs a hasOwnProperty guard.

Can I convert Object.entries() output back into an object?

Yes — pass it to Object.fromEntries(), which is the exact inverse, so entries and fromEntries bracket any array transformation you want to apply to an object.

More examples

Round-trip through fromEntries

Entries out, array filter in the middle, object back in — the standard way to filter an object's fields.

Example · javascript
const stock = { apples: 0, pears: 12, figs: 3 };

const inStock = Object.fromEntries(
  Object.entries(stock).filter(([, n]) => n > 0),
);

console.log(inStock);

Straight into a Map

The Map constructor accepts an iterable of [key, value] pairs, which is exactly what Object.entries() produces.

Example · javascript
const config = { retries: 3, timeout: 500 };
const map = new Map(Object.entries(config));

console.log(map.get("retries"));
console.log(map.size);
console.log([...map.keys()]);

Keys arrive as strings

typeof id is "string" and the strict comparison against the number 2 is false; the entries also come out 2 before 10, in ascending numeric order.

Example · javascript
const byId = { 2: "two", 10: "ten" };

for (const [id, label] of Object.entries(byId)) {
  console.log(typeof id, id, id === 2, Number(id) === 2, label);
}

Discussion

  • Be the first to comment on this lesson.