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.
Demo
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
| Name | Type | Required | Description |
|---|---|---|---|
| iterable | iterable | yes | Anything 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
Object.fromEntries( Object.entries(obj).map(([k, v]) => [k, v * 2]) );
const params = Object.fromEntries(new URLSearchParams(location.search));
const plain = Object.fromEntries(map);
Examples
Pitfalls
Object.fromEntries(new URLSearchParams('t=a&t=b'))
new URLSearchParams('t=a&t=b').getAll('t')
Object.fromEntries(new Map([[1, "a"]]))
const m = new Map([[1, "a"]]);
pairs.reduce((o, [k, v]) => { o[k] = v; return o; }, {})
Object.fromEntries(pairs)
Object.fromEntries(['a', 'b'])
Object.fromEntries(['a', 'b'].map(k => [k, true]))
When to use
- 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
- 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
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);