Simplest way to merge ES6 Maps/Sets?

前端 未结 13 1211
广开言路
广开言路 2020-11-29 17:24

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)?

13条回答
  •  -上瘾入骨i
    2020-11-29 17:48

    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.

提交回复
热议问题