Categories
JavaScript Answers

How to convert Map to array of object with JavaScript?

Spread the love

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.

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 *