inspection

Object.entries

Returns an array of a given object's own enumerable string-keyed [key, value] pairs. Useful for iterating objects with for...of destructuring or converting to a Map.

Object.entries(obj)

Parameters

Parameter Purpose
obj The object whose enumerable own [key, value] pairs are to be returned
returns Array of [key, value] tuples

Examples

console.log(Object.entries({ a: 1, b: 2 }));

Logs [ [ 'a', 1 ], [ 'b', 2 ] ]

for (const [k, v] of Object.entries({ x: 10, y: 20 })) {
  console.log(k, v);
}

Logs 'x 10' then 'y 20'

const map = new Map(Object.entries({ a: 1, b: 2 }));
console.log(map.get('a'));

Logs 1 — object converted to Map

const filtered = Object.entries({ a: 1, b: 2, c: 3 }).filter(([, v]) => v > 1);
console.log(Object.fromEntries(filtered));

Logs { b: 2, c: 3 }

Gotcha

Only own enumerable string keys are returned — Symbols and inherited properties are ignored. Pairs with Object.fromEntries for functional transformations.

Related methods

← All JS Object methods · Array methods · Spread vs rest