Object.entries()
The one to reach for when transforming an object. Paired with Object.fromEntries it gives objects the map and filter they never had.
Demo
Each pair is a two-element array, which is what makes the map-filter-fromEntries pipeline possible — and why callbacks over entries almost always destructure with ([key, value]). The same integer-key reordering applies as with keys and values, but here it does no harm: each key travels with its own value, so nothing can be misaligned. An array yields index-value pairs, with the indices as strings.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| object | object | yes | Any object. Primitives are coerced; null and undefined throw TypeError. |
Return value
[string, any][] — An array of [key, value] pairs for the own enumerable properties, in Object.keys order. Each pair is a two-element array.
Common patterns
const upper = Object.fromEntries( Object.entries(obj).map(([k, v]) => [k, String(v).toUpperCase()]) );
const clean = Object.fromEntries( Object.entries(obj).filter(([, v]) => v != null) );
for (const [key, value] of Object.entries(obj)) { }
Examples
Pitfalls
Object.entries({a: 1}).map((k, v) => k)
Object.entries({a: 1}).map(([k, v]) => k)
Object.fromEntries(Object.entries([10, 20]))
[10, 20].map(x => x)
for (const [k, v] of Object.entries(huge)) { }
for (const k of Object.keys(huge)) { const v = huge[k]; }
Object.fromEntries(Object.entries(instance))
Object.assign(Object.create(Object.getPrototypeOf(i)), i)
When to use
- Mapping or filtering an object property by property
- Iterating where you need both the key and the value
- Converting an object to a Map
- Sorting properties before rebuilding an object
- You only need one side → Object.keys or Object.values
- Very large objects in a hot path → iterate keys and index in
- The source is an array → array methods directly
- You need inherited properties → getOwnPropertyDescriptors, or walk the chain
Notes
FAQ
There is no Object.map. The idiom is entries, then the array method you want, then fromEntries to rebuild. Destructure the pair in the callback and return a new pair.
Object.fromEntries( Object.entries(obj).map(([k, v]) => [k, v * 2]) );