Object.fromEntries()

Builds an object from an iterable of [key, value] pairs, such as an array or a Map.

Signature
Object.fromEntries(iterable)
  • Added in JavaScript ES2019

Parameters

ParameterTypeDescription
$iterableIterableAn iterable, such as an Array or a Map, containing a list of objects. Each object should have two properties: 0 (a string or symbol used as the property key) and 1 (the property value). Typically a two-element array.

Return value

ObjectA new object whose properties are given by the entries of the iterable.

Errors

Throws a TypeError if the argument is not iterable, or if an entry it yields is not an object with 0 and 1 properties.

Object.fromEntries() is the exact inverse of Object.entries(). Give it anything iterable that yields two-element pairs and it hands back a plain object.

When to reach for it

The dominant use is the round trip: Object.entries() out, an array map/filter/sort in the middle, Object.fromEntries() back in. That is how you filter an object's fields, rename its keys, or transform every value without writing a manual accumulator loop.

It is also the shortest conversion from the pair-shaped built-ins: Object.fromEntries(map) converts a Map to an object, and Object.fromEntries(new URLSearchParams(qs)) turns a query string into a settings object. It accepts any iterable, so a generator that yields pairs works too.

What trips people up

Duplicate keys do not merge or error — the last pair silently wins. A Map with non-string keys is coerced: object keys become strings like '[object Object]', which collapses distinct Map entries into one property. And the result is a plain object with Object.prototype attached, so a pair keyed '__proto__' or 'toString' is a real hazard when the input is user data.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Filter an object down to a whitelist of fields and rebuild it in one expression
  • Convert a Map or URLSearchParams into a plain object for JSON serialisation
  • Rename or normalise every key of an API response
  • Transform every value in a settings object while keeping the keys

Frequently asked questions

How do I convert a Map to a plain object?

Call Object.fromEntries(map) — a Map is iterable and yields [key, value] pairs, which is exactly the input format; note that non-string Map keys are coerced to strings.

What happens if the entries contain duplicate keys?

The last occurrence wins and the earlier ones are silently discarded, with no error thrown.

Is Object.fromEntries() the opposite of Object.entries()?

Yes — Object.fromEntries(Object.entries(obj)) reproduces an object with the same own enumerable string-keyed properties, which makes the pair the standard idiom for transforming an object through array methods.

More examples

Pick a whitelist of fields

The secret field is dropped because its key is not in the whitelist; the surviving pairs are reassembled into a new object.

Example · javascript
const record = { id: 1, name: "Ada", secret: "xyz", email: "[email protected]" };
const allowed = ["id", "name", "email"];

const safe = Object.fromEntries(
  Object.entries(record).filter(([k]) => allowed.includes(k)),
);

console.log(safe);

Last duplicate wins

The first output keeps a: 3, silently discarding a: 1. The second collapses two distinct object keys into a single "[object Object]" property.

Example · javascript
console.log(Object.fromEntries([["a", 1], ["b", 2], ["a", 3]]));

// non-string Map keys are coerced to strings
const m = new Map([[{ id: 1 }, "first"], [{ id: 2 }, "second"]]);
console.log(Object.fromEntries(m));

Any iterable of pairs

A generator works because Object.fromEntries() only requires an iterable that yields two-element entries, not an array.

Example · javascript
function* pairs() {
  for (let i = 1; i <= 3; i++) yield [`item${i}`, i * i];
}

console.log(Object.fromEntries(pairs()));

Discussion

  • Be the first to comment on this lesson.