How to convert Map to array of object with JavaScript?

Sometimes, we want to convert Map to array of object with JavaScript.

In this article, we’ll look at how to convert Map to array of object with JavaScript.

How to convert Map to array of object with JavaScript?

To convert Map to array of object with JavaScript, we use the spread operator.

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 and add some entries to it with set.

Next, we spread myMap into an array with the spread operator.

Then we call map with a callback that returns an object with the name and value properties, which is the key and value respectively.

Conclusion

To convert Map to array of object with JavaScript, we use the spread operator.