To convert Map to array of object with JavaScript, we use the spread operator and map
.
For instance, we write
const myMap = new Map().set("a", 1).set("b", 2);
const arr = [...myMap].map(([name, value]) => ({ name, value }));
console.log(arr);
to create a map with the Map
constructor.
Then we call set
to add some key-value pairs.
Next, we spread myMap
into an array of key-value pair arrays.
And then we call map
to get the name
and value
and put them in an object.