EN
JavaScript - convert Map to array of objects
0
points
In this article, we would like to show you how to convert a Map to an array of objects in JavaScript.
Quick solution:
const arrayFromMap = Array.from(myMap, ([key, value]) => ({ key, value }));
Practical example
In this example, we use the Array.from() method to map the key/value pairs from the Map object into an array.
// ONLINE-RUNNER:browser;
const myMap = new Map([
[1, 'A'],
[2, 'B'],
[3, 'C'],
]);
const myArray = Array.from(myMap, ([key, value]) => ({ key, value }));
console.log(JSON.stringify(myArray, null, 4));
Output:
[
{ key: 1, value: 'A' },
{ key: 2, value: 'B' },
{ key: 3, value: 'C' }
]