Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Object.fromEntries

Creates an object from a list of key–value pairs.

Available since: ES2019

Syntax

Object.fromEntries(entries)

Parameters

ParameterTypeDescription
entriesIterableAn array of [key, value] pairs, or a Map.

Return value

objectA new object made from the pairs.

Examples

Pairs become an object:

const pairs = [['name', 'Anna'], ['city', 'Berlin']];
 
console.log(Object.fromEntries(pairs));

Output:

{
  "name": "Anna",
  "city": "Berlin"
}

Transforming object values through entries and fromEntries:

const prices = { tea: 3, coffee: 5 };
const doubled = Object.fromEntries(
  Object.entries(prices).map(([key, value]) => [key, value * 2]),
);
 
console.log(doubled);

Output:

{
  "tea": 6,
  "coffee": 10
}

A Map into a plain object:

const map = new Map([['a', 1], ['b', 2]]);
 
console.log(Object.fromEntries(map));

Output:

{
  "a": 1,
  "b": 2
}

Notes

Together Object.entries() and Object.fromEntries() let you apply array methods to objects: map(), filter(), sort().

See also

  • Object.entries — Returns an array of property name and value pairs.
  • Map — A collection of key–value pairs where a key can be anything.
  • Array.prototype.map — Creates a new array by transforming every element.

Related recipes

In the tutorial

Thanks for reading!

Donate to the site — we work hard for you.