Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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.

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

.map each map to its entries, then flatten the array of arrays of entries to just a single array of entries, then turn that into a Map.

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

const merged = new Map(
  arr.map(
    map => [...map]
  ).flat()
);
console.log([...merged]);

about 4 years ago · Santiago Gelvez Report

0

You are looking for flatMap:

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

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

console.log(merged)

about 4 years ago · Santiago Gelvez Report

0

You can use array#reduce to merge multiple Map.

maps.reduce((r, map) => new Map([...r, ...map]), new Map())

const map1 = new Map();
map1.set('a', 1);
const map2 = new Map();
map2.set('b', 2);
const map3 = new Map();
map3.set('c', 3);

const maps = [map1, map2, map3],
      merged = maps.reduce((r, map) => new Map([...r, ...map]), new Map());
console.log([...merged]);

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!