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
Object.keys
Returns an array of a given object's own enumerable string-keyed property names. The order matches a normal for...in loop but excludes inherited and non-enumerable properties.
Object.values
Returns an array of a given object's own enumerable string-keyed property values. Introduced in ES2017 as a companion to Object.keys.
Object.fromEntries
Transforms a list of key-value pairs into an object — the inverse of Object.entries. Introduced in ES2019 and accepts any iterable of [key, value] pairs, including Map.
Object.hasOwn
Returns true if the specified object has the given property as its own property (not inherited). Added in ES2022 as the recommended replacement for Object.prototype.hasOwnProperty.
Object.prototype.hasOwnProperty
Instance method returning true if the object has the specified property as its own (non-inherited) property. Superseded by the safer static Object.hasOwn in ES2022.