The Object.fromEntries()
static method transforms a list of key-value pairs into an object.
The Object.fromEntries()
static method transforms a list of key-value pairs into an object.
Object.fromEntries(iterable)
A new object whose properties are given by the entries of the iterable.
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. The method returns an iterator object that produces two-element array-like objects. The first element is a value that will be used as a property key, and the second element is the value to associate with that property key.
Object.fromEntries()
performs the reverse of Object.entries()
, except that Object.entries()
only returns string-keyed properties, while Object.fromEntries()
can also create symbol-keyed properties.
Note: Unlike Array.from()
, Object.fromEntries()
does not use the value of this
, so calling it on another constructor does not create objects of that type.
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 }
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
fromEntries |
73 | 79 | 63 | 60 | 12.1 | 73 | 63 | 52 | 12.2 | 11.0 | 73 | 1.0 | 12.0.0 |
© 2005–2023 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