michele
michele

Reputation: 26598

Typescript flatten an array of Map

I have an array of Map<string, number>

How can I flat to a single map?

Map<string, number>[] to Map<string, number>

Thank you

Upvotes: 2

Views: 3569

Answers (1)

Markus Dresch
Markus Dresch

Reputation: 5574

You can use Array.reduce and create a new Map with the result:

let map1 = new Map([['a', 1], ['b', 2]]);
let map2 = new Map([['c', 3], ['d', 4]]);
let maps = [map1, map2];

// reduce to a flattened array
let arr = maps.reduce((acc, val) => [...acc, ...val], []);

// create a new map using that array
let map3 = new Map(arr);

Upvotes: 1

Related Questions