1

I have the following code:

const x = new Map([['key', 'value']])

Now, I want to stringify this. I used the following:

JSON.stringify(x)

...which gave me an empty object.

How do you actually perform the stringification on new Map()? Is it something like you first turn it into an object and then stringify it?

I am expecting the output to be {"key": "value"}.

Axel
  • 4,365
  • 11
  • 63
  • 122

2 Answers2

3

You could get the array from the map and stringify the result.

const x = new Map([['key', 'value']])

console.log(JSON.stringify(Array.from(x)));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

If you want to access direct value then you can use this in loop.

console.log(JSON.stringify(x.get('key')));
Sachin Shah
  • 4,503
  • 3
  • 23
  • 50