Back to 30 Seconds Of Code

How can I convert a Map to a JavaScript object and vice versa?

content/snippets/js/s/convert-map-to-object.md

14.0.0863 B
Original Source

Oftentimes, when working with Map objects, we need to convert from or to plain JavaScript objects. This can be useful when you need to use the Map's key-value pairs as an object or vice versa.

Convert a Map to an object

Using Map.prototype.entries(), we can convert a Map to an array of key-value pairs. Then, we can use Object.fromEntries() to convert the array to an object.

js
const mapToObject = map => Object.fromEntries(map.entries());

mapToObject(new Map([['a', 1], ['b', 2]])); // {a: 1, b: 2}

Convert an object to a Map

Similarly, using Object.entries(), we can convert an object to an array of key-value pairs. Then, we can use the Map() constructor to convert the array to a Map.

js
const objectToMap = obj => new Map(Object.entries(obj));

objectToMap({a: 1, b: 2}); // Map {'a' => 1, 'b' => 2}