Object.fromEntries()
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.
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()
.
With
Object.fromEntries
, you can convert from
Map
to
Object
:
const map = new Map([ ['foo', 'bar'], ['baz', 42] ]);
const obj = Object.fromEntries(map);
console.log(obj); // { foo: "bar", baz: 42 }
With
Object.fromEntries
, you can convert from
Array
to
Object
:
const arr = [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ];
const obj = Object.fromEntries(arr);
console.log(obj); // { 0: "a", 1: "b", 2: "c" }
With
Object.fromEntries
, its reverse method
Object.entries()
,和
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 }
Please do not add polyfills on MDN pages. For more details, refer to: https://discourse.mozilla.org/t/mdn-rfc-001-mdn-wiki-pages-shouldnt-be-a-distributor-of-polyfills/24500
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Object.fromEntries' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fromEntries
|
Chrome 73 | Edge 79 | Firefox 63 | IE No | Opera 60 | Safari 12.1 | WebView Android 73 | Chrome Android 73 | Firefox Android 63 | Opera Android No | Safari iOS 12.2 | Samsung Internet Android No | nodejs 12.0.0 |
完整支持
不支持
Object.entries()
Object.keys()
Object.values()
Map.prototype.entries()
Map.prototype.keys()
Map.prototype.values()
Object
Object.assign()
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.seal()
Object.setPrototypeOf()
Object.values()
Function