Skip to content
Advertisement

JS merge Array of the Maps to a single Map

I have a structure of

Array(4) [Map(1),Map(1),Map(1),Map(1)]

All keys are different there. I am trying find the common way to merge it in one Map.

I know the way for two Maps:

let merged = new Map([...first, ...second])

But for this solution I need more common way.

Advertisement

Answer

You are looking for flatMap:

const arr = [
  new Map([[1, 2]]),
  new Map([[3, 4]]),
];

const merged = arr.flatMap(e => [...e])

console.log(merged)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement