将数组中所有值加入 Set
的旧方式是:
// for the sake of this example imagine this set was created somewhere else
// and I cannot construct a new one out of an array
let mySet = new Set()
for(let item of array) {
mySet.add(item)
}
有没有更优雅的方法来做这个?也许是mySet.add(array)
或mySet.add(...array)
?
PS:我知道两者都不行
Set.prototype.addAll
有一个一阶段的提案,详情请见Set.prototype.addAll
。 - Yury TarabankoSet#add
中。肯定有人认为Array#push
接受多个参数是不好的。 - Robert