The Object.fromEntries()
method transforms a list of key-value pairs into an object.
Object.fromEntries()
Try it
Syntax
Object.fromEntries(iterable);
Parameters
-
iterable
-
An iterable such as
Array
orMap
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
Converting an Array to an Object
Object transformations
With Object.fromEntries
, its reverse method Object.entries()
, and array manipulation methods, you are able to transform objects like this:
const object1 = { a: 1, b: 2, c: 3 }; const object2 = Object.fromEntries( Object.entries(object1) .map(([ key, val ]) => [ key, val * 2 ]) ); console.log(object2); // { a: 2, b: 4, c: 6 }
Specifications
Browser compatibility
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
fromEntries |
73
|
79
|
63
|
No
|
60
|
12.1
|
73
|
73
|
63
|
No
|
12.2
|
11.0
|
1.0
|
12.0.0
|
See also
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries