JavaScript: Object.fromEntries

By Xah Lee. Date: . Last updated: .

New in JS2019.

Object.fromEntries(iterableX)
Convert iterableX to data object. Return a object with key value pairs from Iterable iterableX. The iterableX must have pairs as element, such as nested array, or map. [see the Map Object Tutorial]
// convert nested array of tuple to object
const myArray = [["a", 3], ["b", 4]];
const myObj = Object.fromEntries(myArray);
console.log(myObj);
// { a: 3, b: 4 }
// convert map to object
const myMap = new Map([["a", 3], ["b", 4]]);
const myObj = Object.fromEntries(myMap);
console.log(myObj);
// { a: 3, b: 4 }

JS2015 Map to Object

Here's a function to convert map object to map object. [see JS: Convert Object to/from Map]

/* [
xah_map_to_obj convert a map object to map, require just ES2015.
This assumes the map key are string or symbol, because object key can only be these types.
http://xahlee.info/js/js_Object.fromEntries.html
Version 2020-12-12
] */
const xah_map_to_obj = ((iterable) => {
  const obj = {};
  for (let [k, v] of iterable) obj[k] = v;
  return obj;
});

// HHH___________________________________________________________________
// test
const myMap = new Map([["a", 3], ["b", 4]]);
const myObj = xah_map_to_obj(myMap);

console.log(myObj);
// { a: 3, b: 4 }
JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS Obj Ref

Object Object

prototype