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:
JavaScript
x
11
11
1
[
2
{
3
"name": "a",
4
"value": "1",
5
},
6
{
7
"name": "b",
8
"value": "2",
9
}
10
]
11
Advertisement
Answer
You could take Array.from
and map the key/value pairs.
JavaScript
1
4
1
let map = new Map().set('a', 1).set('b', 2),
2
array = Array.from(map, ([name, value]) => ({ name, value }));
3
4
console.log(array);