Categories
JavaScript Answers

How to Convert a Map to JSON Object with JavaScript?

Spread the love

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.

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 *