Object.fromEntries()

The inverse of Object.entries, added two years later. It also converts anything pair-shaped — a Map, a query string, a form — into a plain object in one call.

Object static methodES2019Live demo
Common call
Object.fromEntries(pairs)
Returns
a new plain object
Replaces
a reduce that assigns into an accumulator
Watch out
duplicate keys silently keep the LAST value
Object.fromEntries(iterableiterable — Anything iterable that yields two-element entries — an array of pairs, a Map, URLSearchParams, FormData. Keys are coerced to strings.type: iterable · required)
→ object

Demo

Live evaluation
Try:
Inputs
jsonstringJSON array of pairs, e.g. [["a",1]]
Output
Object.fromEntries(JSON.parse('[["a",1],["b",2]]'))
{ a: 1, b: 2 }

Each pair becomes one property. The second case is the behaviour to know: a repeated key is not an error and does not merge — the later value simply overwrites the earlier one, so information is lost silently. That matters when building an object from a query string where a parameter legitimately appears more than once. Numeric keys become strings, because every plain-object key is a string, and values of any type are stored as-is.

Parameters

NameTypeRequiredDescription
iterableiterableyesAnything iterable that yields two-element entries — an array of pairs, a Map, URLSearchParams, FormData. Keys are coerced to strings.

Return value

object — A new plain object built from [key, value] pairs. Any iterable of pairs works — an array, a Map, URLSearchParams, FormData.

Common patterns

Transform an object
The round trip that gives objects a map.
Object.fromEntries(
  Object.entries(obj).map(([k, v]) => [k, v * 2])
);
Query string to object
URLSearchParams is already pair-shaped.
const params = Object.fromEntries(new URLSearchParams(location.search));
Map to plain object
For JSON serialisation, which ignores Maps.
const plain = Object.fromEntries(map);

Examples

1. From pairs
Object.fromEntries([['a', 1], ['b', 2]])
Returns
{a: 1, b: 2}
2. Last key wins
Object.fromEntries([['a', 1], ['a', 2]])
Returns
{a: 2}
3. From a Map
Object.fromEntries(new Map([['a', 1]]))
Returns
{a: 1}
4. Round trip
Object.fromEntries(Object.entries({a: 1}))
Returns
{a: 1}
5. Numeric keys
Object.fromEntries([[1, 'x']])
Returns
{'1': 'x'}
6. Empty
Object.fromEntries([])
Returns
{}

Pitfalls

1. Duplicate keys silently collapse
The last pair wins and the rest vanish without warning. A query string like ?tag=a&tag=b becomes a single tag, which is the classic way this loses real data — URLSearchParams has getAll precisely because repeats are legitimate.
One value survives
Object.fromEntries(new URLSearchParams('t=a&t=b'))
{t: 'b'}
Collect them
new URLSearchParams('t=a&t=b').getAll('t')
['a', 'b']
2. It builds a plain object, losing the source type
A Map converted this way gives up insertion order for numeric-looking keys, non-string keys, and its size property. Convert for serialisation, not as a general-purpose replacement.
Key coerced
Object.fromEntries(new Map([[1, "a"]]))
{'1': 'a'} // now a string key
Keep the Map
const m = new Map([[1, "a"]]);
numeric key preserved
3. A prototype-polluting key is a real hazard
Building an object from untrusted pairs lets an attacker supply __proto__ as a key. fromEntries defines an own property rather than assigning, so it does NOT pollute the prototype — but code that reduces into an accumulator with obj[k] = v does. Know which one you are using.
Assignment pollutes
pairs.reduce((o, [k, v]) => { o[k] = v; return o; }, {})
__proto__ reaches the prototype
fromEntries is safe
Object.fromEntries(pairs)
own property named __proto__
4. Entries must be pair-shaped
A flat array of keys, or an iterable of anything that is not indexable, throws. Each item needs an element 0 and an element 1.
Not pairs
Object.fromEntries(['a', 'b'])
TypeError: Iterator value a is not an entry object
Make pairs
Object.fromEntries(['a', 'b'].map(k => [k, true]))
{a: true, b: true}

When to use

Use it
  • Rebuilding an object after mapping or filtering its entries
  • Converting URLSearchParams or FormData into a plain object
  • Converting a Map for JSON serialisation
  • Building an object from a list of computed pairs
Reach for something else
  • Keys may repeat and all values matter → collect into arrays yourself
  • You need non-string keys or guaranteed order → keep the Map
  • You are merging objects → Object.assign or spread
  • The source is already an object and unchanged → no conversion needed

Notes

Complexity
O(n) in the number of entries
Return
A new plain object with Object.prototype as its prototype
CPython impl
V8: Builtins-object-fromentries
Memory
Allocates the resulting object
Thread-safe
Single-threaded; the iterable is consumed once

FAQ

Not with fromEntries — it is specified to overwrite. Group first, so each key maps to an array, then build the object from those pairs.

const grouped = Object.groupBy(pairs, ([k]) => k);

History

ES2019
Object.fromEntries added to complete the round trip with Object.entries.