How to convert Map to array of object?

JavascriptArrays

Javascript Problem Overview


i'm trying to convert a Map into array of object

Lets say I have the following map:

let myMap = new Map().set('a', 1).set('b', 2);

And I want to convert the above map into the following:

[   {      "name": "a",      "value": "1",   },   {      "name": "b",      "value": "2",   }]

Javascript Solutions


Solution 1 - Javascript

You could take Array.from and map the key/value pairs.

let map = new Map().set('a', 1).set('b', 2), array = Array.from(map, ([name, value]) => ({ name, value }));

console.log(array);

Solution 2 - Javascript

Use Spread syntax and Array.map():

let myMap = new Map().set('a', 1).set('b', 2);

const arr = [...myMap].map(([name, value]) => ({ name, value }));

console.log(arr);

Solution 3 - Javascript

with spread of ES6 like this:

let myMap = new Map().set('a', 1).set('b', 2);

const  result = Array.from(myMap).map(([name, value]) => ({name, value}))

console.log(result);

Solution 4 - Javascript

You can also simply iterate over the Map object using any compatible iteration method and add each item as an object to the resulting array.

For example, you can use Map.prototype.forEach() in the following way:

const myMap = new Map().set('a', 1).set('b', 2);
const arr = [];

myMap.forEach((value, name) => arr.push({ name, value }));

console.log(arr);

Or, as an alternative, you may use the for...of loop like so:

const myMap = new Map().set('a', 1).set('b', 2);
const arr = [];

for (const [name, value] of myMap) {
    arr.push({ name, value });
}

console.log(arr);

Wrote a blog post explaining different methods for those who're interested in learning more.

Solution 5 - Javascript

With ES6 Spread syntax only :

const myMap = new Map().set('a', 1).set('b', 2);
const arr = [...myMap];
console.log(arr);

Solution 6 - Javascript

Very simple I will explain in steps:

  1. Spread the map in to array of arrays.
const map = new Map().set('a', 1).set('b', 2).set('c', 3)
const mapArr = [...map] // array of arrays
  1. Map each subarray as key-value pairs. map function returns a new array containing object of key-value pairs.
const arr = mapArr.map(([key, value]) => ({key, value}))

** Remember: ** the name you use for mapping the data will be the names of the object mapped in this case key and value.

One liner:

const map = new Map().set('a', 1).set('b', 2).set('c', 3)
const arr = [...map ].map(([key, value]) => ({key, value}))
console.log({arr})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNikhil ShresthaView Question on Stackoverflow
Solution 1 - JavascriptNina ScholzView Answer on Stackoverflow
Solution 2 - JavascriptFractionView Answer on Stackoverflow
Solution 3 - JavascriptGhoul AhmedView Answer on Stackoverflow
Solution 4 - JavascriptdesignciseView Answer on Stackoverflow
Solution 5 - JavascriptHoucine AdsensView Answer on Stackoverflow
Solution 6 - JavascriptTeocciView Answer on Stackoverflow