Files
30-seconds-of-code/snippets/js/s/convert-map-to-object.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

519 B

title, type, shortTitle, language, tags, author, cover, dateModified
title type shortTitle language tags author cover dateModified
Convert Map to object snippet Map to object javascript
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}