Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Object

Object.fromEntries()

Reference for JavaScript Object.fromEntries(): build an object from key-value pairs, convert a Map or URLSearchParams, and transform objects with entries().

Object.fromEntries() takes an iterable of [key, value] pairs and returns a new object with those properties. It is the inverse of Object.entries().

Typical uses are converting a Map to a plain object, turning a list of pairs into a lookup table, and finishing a "map over an object" pipeline.

JavaScript
Object.fromEntries([["a", 1], ["b", 2]]); // { a: 1, b: 2 }

Try it

JS Pairs, Maps and a pipeline

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Duplicate keys and odd keys

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
Object.fromEntries(iterable)
Name Description
iterable An iterable (array, Map, generator, URLSearchParams, ...) that yields objects shaped like [key, value].

Return value: a new plain object.

Exceptions: a TypeError if the argument is not iterable, or if an item is not an object (for example a number).

Usage notes

  • Later keys win. If a key appears twice, the last value is kept, as with repeated assignment.
  • Keys are converted to property keys. Numbers, booleans and null become strings (1 becomes "1"). Symbols stay symbols.
  • Items need only be array-like objects: the first element is read as the key and the second as the value. Extra elements are ignored.
  • Properties are plain and writable. They are created like normal assignments (enumerable, writable, configurable), so a key "__proto__" becomes an own property rather than changing the prototype.
  • Repeated keys in URLSearchParams keep only the last value when converted. Use getAll() if you need all of them.
  • Map keys that are objects are converted to strings such as "[object Object]". Use a Map if you need object keys.
  • Shallow: values are not cloned.

Browser support

Object.fromEntries() is supported in all current browsers and JavaScript runtimes.