es6去重Set方法 & ...扩展运算符 合并数组

2019-04-30  本文已影响0人  Christoles

去重

    var arr = [1,2,3,2,4,3,5] ;

去重方法1 --- es6去重-----数组变成一个去重了的对象-

    var newData = new Set(this.data.arr);
      console.log("去重后", newData)//{1, 2, 3, 4, 5}
    var newArr = Array.from(newData);
      console.log("再变回数组", newArr)//[1, 2, 3, 4, 5]

去重方法2 --- ...扩展运算符

    var newArr2 = [...new Set(this.data.arr)]
      console.log(newArr2)//[1, 2, 3, 4, 5]

... 扩展运算符

合并数据 --- 一次解构一层

    var arr1 = [1,2,3,...[7,8,9]];
    var arr2 = [4,5,6];
    var arr3 = [...arr1,...arr2];
      console.log(arr3)//[1,2,3,7,8,9,4,5,6]

... 扩展运算符 拓展:
可以取单个数,剩下的为一个数组

    fun(1,2,3,4,5,6)
    function fun(num,num2,...list){
      console.log(num) //1
      console.log(num2) //2
      console.log(list) //[3,4,5,6]
    }
    
//解构赋值 声明法
            let [a,b,c] = [1,2,3];
            let {e,f,h} = {test:"sss",e:11,msg:"hello",f:22,h:33};
                console.log([a,b,c])//[1, 2, 3]
                console.log({a,b,c})//{a: 1, b: 2, c: 3}
                console.log({e,f,h})//{e: 11, f: 22, h: 33}
                console.log(a,b,c)//1 2 3
                console.log(e,f,h)//11 22 33
                
            function add(num1=12,num2=num1){
                return num1*num2
            }
            console.log(add())//144
            console.log(add(2))//4
            
            function add1(num1=num2,num2=2){
                return num1*num2
            }
上一篇下一篇

猜你喜欢

热点阅读