es6-补充项

1、 解构赋值

  • 交换变量

    let x = 1;
    let y = 2;
    
    [x, y] = [y, x];
    
  • 函数返回多个值

    // 返回一个数组
    function example() {
        return [1, 2, 3];
      }
      let [a,  b, c] = example();
    // 返回一个对象
    function example() {
        return {
          foo: 1,
          bar: 2
          }
    }  
    let { foo, bar } = example();
    
  • 函数参数的定义

    // 参数是一组有次序的值
    function f([x, y, z]) { }
    f([1, 2, 3]);
    // 参数是一组无次序的值
    function f({x, y, z}) { }
    f({z: 3, y: 2, x:  1});
    
  • 提取 JSON 数据

    let jsonData = {
        id: 42,
        status: "OK",
        data: [867, 5309]
      };
      let { id, status, data: number } = jsonData;
      console.log(id, status, number); 42, "OK", [867, 5309]
    

2、扩展运算符

  • 输出

    console.log(...[1, 2, 3])
    // 1 2 3
    console.log(1, ...[2, 3, 4], 5)
    // 1 2 3 4 5
    
  • 函数调用

    //情形1
    function add(x, y) {
        return x + y;
      }
      
      const numbers = [4, 38];
      add(...numbers) // 42
      //情形2
      function push(array, ...items) {
    	array.push(...items);
      }
    
  • 将一个数组添加到数组尾部

    let arr1 = [0, 1, 2];
    let arr2 = [3, 4, 5];
    arr1.push(...arr2);
    
  • 数组合并

    const arr1 = ['a', 'b'];
    const arr2 = ['c'];
    const arr3 = ['d', 'e'];
    
    // ES6 的合并数组
    [...arr1, ...arr2, ...arr3]
    //[ 'a', 'b', 'c', 'd', 'e' ]
    

3、对象

  • 判断两个值是否相等

    Object.is('foo', 'foo')
    // true
    
  • 对象合并

    const target = { a: 1 };
    const source1 = { b: 2 };
    const source2 = { c: 3 };
    Object.assign(target, source1, source2);
    target // {a:1, b:2, c:3}
    
  • 获取对象键值

    let obj = { a: 1, b: 2, c: 3 };
    
    for (let key of keys(obj)) {
      console.log(key); // 'a', 'b', 'c'
    }
    
    for (let value of values(obj)) {
      console.log(value); // 1, 2, 3
    }
    
    for (let [key, value] of entries(obj)) {
      console.log([key, value]); // ['a', 1], ['b', 2], ['c', 3]
    }
    

4、set和map

  • 数组去重

    const arr = [21,3,3,4];
    const a = new Set([...arr]);//a:Set { 21, 3, 4 }
    console.log([...a]);//Set { 21, 3, 4 } -> [ 21, 3, 4 ]
    
  • 字符串去重

    let str = 'abbbccd';
    const a =new Set(str);//a:Set { 'a',  'b', 'c', 'd' }
    const joinA = [...a].join('');//[...a] -> set转为数组,
    console.log(joinA);
    console.log([...a])//Set { 'a', 'b', 'c', 'd' } -> [ 'a', 'b', 'c', 'd' ]
    
  • 数组的交集

    let a = new Set([1, 2, 3]);
    let b = new Set([4, 3, 2]);
    
    // 并集
    let union = new Set([...a, ...b]);
    console.log(union);//Set { 1, 2, 3, 4 }
    
    // 交集
    let intersect = new Set([...a].filter(x => b.has(x)));
    console.log(intersect);// set {2, 3}
    // (a 相对于 b 的)差集
    let difference = new Set([...a].filter(x => !b.has(x)));
    // Set {1}
    
  • set转化为数组

    const items = new Set([1, 2, 3, 4, 5]);
    const array = Array.from(items);
    console.log(array);
    
    //方式二:[...Arr]
    console.log([...items])
    
  • set属性

    let arr = [1,12,1,3,4,2,4,3];
    let setArr = new Set([...arr]);
    console.log(setArr);//Set { 1, 12, 3, 4, 2 }
    console.log('set对象的size:',setArr.size);//set对象的size: 5
    setArr.add(6)
    console.log('set对象的add 6:',setArr);//set对象的add 6: Set { 1, 12, 3, 4, 2, 6 }
    setArr.delete(3)
    console.log('set对象的delete 3:',setArr);//set对象的delete 3: Set { 1, 12, 4, 2, 6 }
    console.log('set对象的has 3:',setArr.has(3));//set对象的has 3: false
    setArr.clear()
    console.log('set对象的 clear:',setArr);//set对象的 clear: Set {}
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值