合并 ES6 地图/集的最简单方法?

2022-01-17 00:00:00 set javascript ecmascript-6

有没有一种简单的方法可以将 ES6 Maps 合并在一起(比如 Object.assign)?既然我们在这里,那么 ES6 集合(如 Array.concat)呢?

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

推荐答案

对于套装:

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

对于地图:

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.

相关文章