How to Convert a Map to JSON Object with JavaScript?

Sometimes, we want to convert a map to JSON object with JavaScript.

In this article, we’ll look at how to convert a map to JSON object with JavaScript.

How to Convert a Map to JSON Object with JavaScript?

To convert a map to JSON object with JavaScript, we can use the Object.fromEntries method.

For instance, we write:

const map1 = new Map([
  ['foo', 'bar'],
  ['baz', 12]
]);

const obj = Object.fromEntries(map1);
console.log(obj)

then we get that obj is {foo: 'bar', baz: 12} according to the console log.

Conclusion

To convert a map to JSON object with JavaScript, we can use the Object.fromEntries method.