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", } ]
map() can be used to iterate through objects in an array and, in a similar fashion to traditional arrays, modify the content of each individual object and return a new array.
To convert a Map to an object, call the Object. fromEntries() method passing it the Map as a parameter, e.g. const obj = Object. fromEntries(map) . The Object.
To convert an object to an array you use one of three methods: Object. keys() , Object. values() , and Object. entries() .
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With