Convert Map to object

Angelos Chalaris · JavaScript, Object · Jun 16, 2022

Converts a Map to an object.

  • Use Map.prototype.entries() to convert the Map to an array of key-value pairs.
  • Use Object.fromEntries() to convert the array to an object.
const mapToObject = map => Object.fromEntries(map.entries());
mapToObject(new Map([['a', 1], ['b', 2]])); // {a: 1, b: 2}

Recommended snippets