es6拓展运算符

//拓展运算符 ... 将一个数组转为用逗号分隔的参数序列 能够展开字符串,数组,对象
//数组克隆 引用类型直接= 引用的内存是是同一个,利用拓展运算符进行克隆是开辟了一块新的内存
const arr = [1, 2, 3, 4];
const arr1 = [...arr]
console.log(arr1) //[1,2,3,4]
//数组合并
const arr1 = [1, 2, 3, 4];
const arr2 = [5, 6];
const arr3 = [7, 8, 9];
const arr4 = [...arr1, ...arr2, ...arr3];
//等价于
const arr4 = arr1.concat(arr2, arr3);
console.log(arr4) //[1,2,3,4,5,6,7,8,9]
//数组拼接
const arr1 = [1, 2, 3, 4];
const arr2 = [4, 5, 6];
arr1.push(...arr2);
console.log(arr1) //[1,2,3,4,5,6]
//字符串数组
const string = 'hello,world';
const arr = [...string];
//等价于
const arr = string.split('');
//与解构赋值结合 只能放在参数最后一位
const [fisrt, ...second] = [1, 2, 3, 4, 5];
console.log(second) //[2,3,4,5]
//类数组转换成真正的数组
let argu = function () {
    console.log(arguments)
    let list = [...arguments];
    console.log(list);
}
argu('a', 'b', 'c', 'd', 'e', 'f');
//剩余参数
let foo = (a, b, ...c) => {
    console.log(a, b, c)
}
foo(1, 2, 3, 4, 5)



const a = [2, 3, 4]
//展开
const b = [1, ...a, 5];
console.log(b);
//新增
let list = [1, 2, 3, 4]
let newList = [...list, 5, 6, 7];
console.log(newList);

//为对象新增属性
let obj = { name: '111', age: 18 };
let resultObj = { ...obj, sex: '男' }
console.log(resultObj)


const people = {
    name: 'Lucy',
    age: 30,
    sex: '女',
    hobbies: ['play games', 'basketball', 'swim']
};

const attr = ['basketball', 'swim']
const result = {
    ...people,
    ...(attr && { attr })
}
console.log(result); // {name: "Lucy", age: 30, sex: "女", hobbies: Array(3), attrs: Array(2)}


const list = [1, 2, 3];
console.log({ list }); //等价于{list:list}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值