Categories
JavaScript Answers

How to convert Map to JSON object in JavaScript?

Spread the love

To convert Map to JSON object in JavaScript, we use the Object.fromEntries method.

For instance, we write

const map1 = new Map([
  ["foo", "bar"],
  ["baz", 42],
]);

const obj = Object.fromEntries(map1);

to create a map with the Map constructor.

Then we call Object.fromEntries with map1 to convert it to an array of key-value pair arrays.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *