Files
30-seconds-of-code/javascript/snippets/map-to-object.md
2023-05-01 22:35:56 +03:00

498 B

title, type, shortTitle, tags, author, cover, dateModified
title type shortTitle tags author cover dateModified
Convert Map to object snippet Map to object
object
chalarangelo succulent-1 2022-06-16T05:00:00-04:00

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}