Object.fromEntries[]
The Object.fromEntries[] method transforms a list of key-value pairs into an object.
Syntax
Parameters
An iterable such as Array or Map or other objects implementing the iterable protocol.
Return value
A new object whose properties are given by the entries of the iterable.
Description
The Object.fromEntries[] method takes a list of key-value pairs and returns a new object whose properties are given by those entries. The iterable argument is expected to be an object that implements an @@iterator method, that returns an iterator object, that produces a two element array-like object, whose first element is a value that will be used as a property key, and whose second element is the value to associate with that property key.
Object.fromEntries[] performs the reverse of Object.entries[].
Examples
Converting a Map to an Object
With Object.fromEntries, you can convert from Map to Object:
Converting an Array to an Object
With Object.fromEntries, you can convert from Array to Object:
Object transformations
With Object.fromEntries, its reverse method Object.entries[], and array manipulation methods, you are able to transform objects like this:
Specifications
ECMAScript Language Specification [ECMAScript] # sec-object.fromentries |
Browser compatibility
BCD tables only load in the browser
See also
- A polyfill of Object.fromEntries is available in core-js
- Object.entries[]
- Object.keys[]
- Object.values[]
- Map.prototype.entries[]
- Map.prototype.keys[]
- Map.prototype.values[]