合并 ES6 地图/集的最简单方法? [英] Simplest way to merge ES6 Maps/Sets?

查看:16
本文介绍了合并 ES6 地图/集的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以将 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.

这篇关于合并 ES6 地图/集的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆