JS: Convert Map Object to Object Object 🚀

By Xah Lee. Date: .

JS: Convert Map Object to Object Object

Here's a function to convert map object to data object.

/*
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 = ((xiterable) => {
  const xout = {};
  for (let [k, v] of xiterable) xout[k] = v;
  return xout;
});

// ssss---------------------------------------------------
// test
const xx = new Map([["a", 3], ["b", 4]]);
const jj = xah_map_to_obj(xx);

console.log(jj);
// { a: 3, b: 4 }