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.
Object.fromEntries([["a", 1], ["b", 2]]); // { a: 1, b: 2 }
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
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
nullbecome strings (1becomes"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
URLSearchParamskeep only the last value when converted. UsegetAll()if you need all of them. Mapkeys that are objects are converted to strings such as"[object Object]". Use aMapif you need object keys.- Shallow: values are not cloned.
Browser support
Object.fromEntries() is supported in all current browsers and JavaScript runtimes.