How to add an array of values to a Set

前端 未结 9 745
轮回少年
轮回少年 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:31

    create a new Set:

        //Existing Set
        let mySet = new Set([1,2,3,4,5]);
        //Existing Array
        let array = [6,7,8,9,0];
            
        mySet = new Set(array.concat([...mySet]));
        console.log([...mySet]);
        
        //or single line
        console.log([...new Set([6,7,8,9,0].concat([...new Set([1,2,3,4,5])]))]);

提交回复
热议问题