Skip to main content

The Object.entries() method

Object.entries() is a built-in JavaScript method that returns an array of [key, value] pairs for all own (non-inherited) enumerable properties of an object. It combines what Object.keys() and Object.values() give you separately, which makes it the main tool for iterating over an object.

Theory

TL;DR

  • Returns an array of arrays, where each inner array is [key, value].
  • Own properties only, and enumerable properties only.
  • The order matches Object.keys() and Object.values().
  • The key in a pair is always a string; the value keeps its type.
  • Fits for...of with destructuring perfectly: for (const [key, value] of ...).
  • The reverse conversion is Object.fromEntries(), and new Map(Object.entries(obj)) builds a Map.

Quick example

javascript
const user = { name: 'Maria', age: 25, city: 'Kyiv' }; console.log(Object.entries(user));

The result:

javascript
[ ['name', 'Maria'], ['age', 25], ['city', 'Kyiv'] ]

Each [key, value] pair is one element of the resulting array.

Syntax

javascript
Object.entries(obj)
  • obj is the object whose properties you want turned into pairs.
  • It returns an array of arrays, where each inner array is [key, value].

Which properties end up in the result

  1. It returns own properties only:

    javascript
    const base = { type: 'human' }; const user = Object.create(base); user.name = 'Maria'; console.log(Object.entries(user)); // [['name', 'Maria']]
  2. It returns enumerable properties only (enumerable: true):

    javascript
    const obj = {}; Object.defineProperty(obj, 'hidden', { value: 42, enumerable: false }); obj.visible = 10; console.log(Object.entries(obj)); // [['visible', 10]]
  3. The order matches Object.keys() and Object.values(): integer-like keys first in ascending order, then the rest in insertion order.

Symbol keys are not returned, just as with its two siblings.

Iterating over an object

javascript
const user = { name: 'Maria', age: 25 }; for (const [key, value] of Object.entries(user)) { console.log(`${key}: ${value}`); }

Output:

javascript
name: Maria age: 25

This is the most readable way to walk an object: unlike for...in, it needs no guard against inherited properties, because they are simply not in the array. Filtering and reshaping are just as convenient:

javascript
const prices = { apples: 120, bread: 45, milk: 80 }; const expensive = Object.entries(prices) .filter(([, price]) => price > 50) .map(([name]) => name); console.log(expensive); // ['apples', 'milk']

Converting an object into a Map

javascript
const user = { name: 'Maria', age: 25 }; const userMap = new Map(Object.entries(user)); console.log(userMap.get('age')); // 25

Object.entries() is the handy way to convert between Object and Map, because the Map constructor takes exactly this array of pairs. The other direction is Object.fromEntries(map).

The reverse conversion: Object.fromEntries

javascript
const entries = [ ['name', 'Maria'], ['age', 25] ]; const user = Object.fromEntries(entries); console.log(user); // { name: 'Maria', age: 25 }

entries plus fromEntries is the standard "map over an object and build a new one" trick:

javascript
const raw = { name: ' Maria ', city: ' Kyiv ' }; const trimmed = Object.fromEntries( Object.entries(raw).map(([key, value]) => [key, value.trim()]) ); console.log(trimmed); // { name: 'Maria', city: 'Kyiv' }

Summary of the three methods

MethodWhat it returnsSees the prototypeData type
Object.keys(obj)an array of keysNoArray<string>
Object.values(obj)an array of valuesNoArray<any>
Object.entries(obj)an array of [key, value] pairsNoArray<[string, any]>

Common mistakes

  • Dropping the brackets in destructuring. It is for (const [key, value] of Object.entries(obj)), not for (const key, value of ...).
  • Expecting an object back. The method returns an array; to get an object again you need Object.fromEntries().
  • Counting on inherited properties. Nothing from the prototype shows up, and neither do non-enumerable or symbol keys.
  • Treating the key as a number. The key in a pair is always a string: Object.entries({ 1: 'a' }) returns [['1', 'a']].
  • Using it on large objects in hot code. The method allocates a new array plus one array per pair, so in a frequently executed loop iterating Object.keys() is cheaper.
  • Confusing it with the array entries(). array.entries() returns an iterator of [index, value] pairs, not an array.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.