How can I Display a JavaScript ES6 Map Object to Console?

newguy picture newguy · Aug 8, 2017 · Viewed 13.8k times · Source

I'm using repl.it/languages/javascript.

Do I have to convert it to an object before I print it out?

I've tried

The results are always empty object.

When I use

console.log([...mapObject]);

It prints out an array format.

Answer

samehanwar picture samehanwar · Sep 7, 2020

there is a more simpler solution you can try.

 const mapObject = new Map();   
 mapObject.set(1, 'hello');

 console.log([...mapObject.entries()]);
 // [[1, "hello"]]

 console.log([...mapObject.keys()]);
 // [1]

 console.log([...mapObject.values()]);
 // ["hello"]