creation

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.fromEntries(iterable)

Parameters

Parameter Purpose
iterable Any iterable yielding [key, value] pairs (e.g. Map, array of pairs)
returns A new object composed of the given entries

Examples

console.log(Object.fromEntries([['a', 1], ['b', 2]]));

Logs { a: 1, b: 2 }

const map = new Map([['x', 10], ['y', 20]]);
console.log(Object.fromEntries(map));

Logs { x: 10, y: 20 }

const params = new URLSearchParams('a=1&b=2');
console.log(Object.fromEntries(params));

Logs { a: '1', b: '2' }

const doubled = Object.fromEntries(
  Object.entries({ a: 1, b: 2 }).map(([k, v]) => [k, v * 2])
);
console.log(doubled);

Logs { a: 2, b: 4 } — the classic map-object transform pattern

Gotcha

Later entries with the same key overwrite earlier ones. Keys are coerced to strings unless they are Symbols; number keys become string keys.

Related methods

← All JS Object methods · Array methods · Spread vs rest