Simplest way to merge ES6 Maps/Sets?

jameslk picture jameslk · Aug 14, 2015 · Viewed 119.4k times · Source

Is there a simple way to merge ES6 Maps together (like Object.assign)? And while we're at it, what about ES6 Sets (like Array.concat)?

Answer

Oriol picture Oriol · Aug 14, 2015

For sets:

var merged = new Set([...set1, ...set2, ...set3])

For maps:

var merged = new Map([...map1, ...map2, ...map3])

Note that if multiple maps have the same key, the value of the merged map will be the value of the last merging map with that key.