OBJECT.FROMENTRIES()
ECMAScript 2019 added a static method fromEntries()
to the Object
class which builds an object from a collection of key-value array pairs. This method performs the opposite operation of Object.entries()
. This method is demonstrated here:
const obj = {
foo: 'bar',
baz: 'qux'
};
const objEntries = Object.entries(obj);
console.log(objEntries);
// [["foo", "bar"], ["baz", "qux"]]
console.log(Object.fromEntries(objEntries));
// { foo: "bar", baz: "qux" }
The static method expects an iterable object containing any number of iterable objects of size 2. This is especially useful in cases where you wish to convert a Map
instance to an Object
instance, as the Map
iterator's output exactly matches the signature that fromEntries()
ingests:
const map = new Map().set('foo', 'bar');
console.log(Object.fromEntries(map));
// { foo: "bar" }