How to add an array of values to a Set

前端 未结 9 653
轮回少年
轮回少年 2020-12-29 02:15

The old school way of adding all values of an array into the Set is:

// for the sake of this example imagine this set was created somewhere else 
// and I ca         


        
9条回答
  •  一生所求
    2020-12-29 02:38

    While Set API is still very minimalistic, you can use Array.prototype.forEach and shorten your code a bit:

    array.forEach(item => mySet.add(item))
    
    // alternative, without anonymous arrow function
    array.forEach(mySet.add, mySet)
    

提交回复
热议问题