How to convert a Map into an Object in JavaScript?

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Even though a Map looks like a regular object in JavaScript, converting from one to the other isn't always intuitive.

Article published on 28/03/2024, last updated on 10/08/2026

In JavaScript, a Map is a native class (since ES6) that makes it easy to create data dictionaries!

If you're wondering what the differences are between a JSON object (key-value) and a Map, you can read this article.

nicolasbrondinbernard_A_Dictionnary._100_white_background._styl_f7e37200-da05-4159-9eec-5d58b68a2795.png

Let's take a very simplified example dictionary, like this:

const myMap = new Map();

myMap.set("a", "alternative");
myMap.set("b", "binoculaire");
myMap.set("c", "cyclomoteur");

To convert it to an Object type, simply use the fromEntries method of the Object class, like this:

const myObject = Object.fromEntries(myMap);

console.log(myObject);
// Output: { a: "alternative", b: "binoculaire", c: "cyclomoteur" }

And there you go, your dictionary is now stored in a classic JSON object!

Careful: The conversion only happens on a single level of depth, so if your Map contains other Maps, then your final Object will also contain those Maps.


Finished reading this article?
Our complete courses
Take it to the next level with our courses!

Complete courses, exercises and certificates to really learn programming!

4.8 average rating

Comments (0)

to leave a comment

No comments yet