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()andObject.values(). - The key in a pair is always a string; the value keeps its type.
- Fits
for...ofwith destructuring perfectly:for (const [key, value] of ...). - The reverse conversion is
Object.fromEntries(), andnew Map(Object.entries(obj))builds aMap.
Quick example
const user = {
name: 'Maria',
age: 25,
city: 'Kyiv'
};
console.log(Object.entries(user));The result:
[
['name', 'Maria'],
['age', 25],
['city', 'Kyiv']
]Each [key, value] pair is one element of the resulting array.
Syntax
Object.entries(obj)objis 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
-
It returns own properties only:
javascriptconst base = { type: 'human' }; const user = Object.create(base); user.name = 'Maria'; console.log(Object.entries(user)); // [['name', 'Maria']] -
It returns enumerable properties only (
enumerable: true):javascriptconst obj = {}; Object.defineProperty(obj, 'hidden', { value: 42, enumerable: false }); obj.visible = 10; console.log(Object.entries(obj)); // [['visible', 10]] -
The order matches
Object.keys()andObject.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
const user = { name: 'Maria', age: 25 };
for (const [key, value] of Object.entries(user)) {
console.log(`${key}: ${value}`);
}Output:
name: Maria
age: 25This 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:
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
const user = { name: 'Maria', age: 25 };
const userMap = new Map(Object.entries(user));
console.log(userMap.get('age')); // 25Object.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
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:
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
| Method | What it returns | Sees the prototype | Data type |
|---|---|---|---|
Object.keys(obj) | an array of keys | No | Array<string> |
Object.values(obj) | an array of values | No | Array<any> |
Object.entries(obj) | an array of [key, value] pairs | No | Array<[string, any]> |
Common mistakes
- Dropping the brackets in destructuring. It is
for (const [key, value] of Object.entries(obj)), notfor (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 readyA concise answer to help you respond confidently on this topic during an interview.