Reference

Reference · Objects

Object.entries()

Object.entries() returns an array of an object's own [key, value] pairs — the cleanest way to iterate an object when your loop needs both the key and the value.

XCODXReference · Updated

Definition

Object.entries() returns an array of [key, value] pairs for an object's own enumerable properties. Each element is a two-item array: the key first, the value second. It is the method to reach for whenever a loop needs *both* the key and the value, because it hands you both together without a second lookup.

It completes the trio with Object.keys() and Object.values(): same rules about own enumerable properties, same order, but returning pairs instead of just keys or just values. Pairs are also the exact shape a Map accepts, which makes Object.entries() the bridge between plain objects and maps.

The pairing is what makes Object.entries() special. Object.keys() forces a second lookup — obj[key] — every time you need the value; entries hand you both halves at once, which is not only more convenient but slightly faster in a tight loop and, more importantly, impossible to get wrong by mistyping the key. Whenever a loop or transform touches both the key and the value, entries is the right starting point.

Syntax

Object.entries(obj)

// obj: the object whose own enumerable [key, value] pairs you want
// returns: a new array of [key, value] arrays

Parameters

ParameterRequiredDescription
objYesThe object to read entries from. Like Object.keys(), it reads only own enumerable string-keyed properties.

Object.entries() is a static method, called as Object.entries(obj). Combined with array destructuring in a for...of loop — for (const [key, value] of ...) — it gives the clearest possible object iteration.

Return value

A new array of [key, value] two-element arrays, one per own enumerable property. An object with no such properties returns an empty array. The original object is not modified.

  • Pairs follow the same order as Object.keys(): integer-like keys first, then insertion order.
  • Each value is the property's value as-is (a reference, for objects and arrays).
  • Symbol-keyed and inherited properties are excluded.

Examples

Iterate with destructuring

Transform an object

const prices = { pen: 3, book: 12 };

// double every value, back into an object
const doubled = Object.fromEntries(
  Object.entries(prices).map(([k, v]) => [k, v * 2])
);
console.log(doubled); // { pen: 6, book: 24 }

Filter properties by value

const stock = { pen: 0, book: 12, ink: 5 };

const inStock = Object.fromEntries(
  Object.entries(stock).filter(([, qty]) => qty > 0)
);
console.log(inStock); // { book: 12, ink: 5 }

Convert an object to a Map

const config = { debug: true, level: 3 };

const map = new Map(Object.entries(config));
console.log(map.get('level')); // 3

Browser & runtime support

The entries → transform → fromEntries pattern

Objects have no map or filter of their own, so the idiomatic way to transform one is a round trip: Object.entries() turns it into an array of pairs, you map/filter those pairs with ordinary array methods, and Object.fromEntries() rebuilds an object from the result. Once this pattern clicks, most "how do I map over an object" questions answer themselves — you map over its entries and convert back.

Object.fromEntries() is not only for round-trips. It turns *any* list of pairs into an object, so it also shapes data arriving from other sources: a Map (Object.fromEntries(map)), URL query parameters (Object.fromEntries(new URLSearchParams(qs))), or a hand-built array of [key, value] pairs. Wherever key–value data shows up, fromEntries is the one-liner that turns it into a plain object.

A small idiom worth recognising: because each entry is a two-element array, you destructure it in the callback head and can ignore a half you don't need with an empty slot. entries.filter(([, value]) => value > 0) keeps the comma but skips the key; entries.map(([key]) => key) takes only the key. This comma-hole pattern appears constantly in entries code.

Availability

Object.entries() and Object.values() were added in ES2017 and are available in every current browser and maintained Node.js release. Object.fromEntries(), its inverse, arrived in ES2019 and is equally well supported today.

Frequently asked questions

How do I iterate over an object with both key and value?
Use for (const [key, value] of Object.entries(obj)). The array destructuring gives you both halves of each property in one clean loop.
How do I map over an object in JavaScript?
Objects have no map(), so use the entries round-trip: Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, transform(v)])).
What's the difference between Object.entries() and Object.keys()?
Object.keys() returns an array of keys; Object.entries() returns an array of [key, value] pairs. Use entries when the loop needs the value as well as the key.
How do I turn an object into a Map?
Pass its entries to the Map constructor: new Map(Object.entries(obj)). Object.entries() produces exactly the [key, value] pairs a Map expects.