扩展操作符 … 是es6中引入的,将可迭代对象展开到其单独的元素中,所谓的可迭代对象就是任何能用for of循环进行遍历的对象,例如:数组、字符串、map 、set 、dom节点等。
1. 拷贝数组对象使用扩展符拷贝数组是es6中常用的操作:
const years = [2018, 2019, 2020, 2021];const copyyears = [...years];console.log(copyyears); // [ 2018, 2019, 2020, 2021 ]
扩展运算符拷贝数组,只有第一层是深拷贝,即对一维数组使用扩展运算符拷贝就属于深拷贝,看下面的代码:
const minicalendar = [2021, [1, 2, 3, 4, 5, 6, 7], 1];const copyarray = [...minicalendar];console.log(copyarray); // [ 2021, [ 1, 2, 3, 4, 5, 6, 7 ], 1 ]copyarray[1][0] = 0;copyarray[1].push(8);copyarray[2] = 2;console.log(copyarray); // [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 2 ]console.log(minicalendar); // [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 1 ]
把打印的结果放在一起便于更加清楚进行对比,如下:
变量说明结果操作
copyarray [ 2021, [ 1, 2, 3, 4, 5, 6, 7 ], 1 ] 复制数组 minicalendar
copyarray [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 2 ] 1. 将数组第二个元素的第一个元素重新赋值为 0 ;2. 往数组的第二个元素增加一个元素 8 ;3. 将数组第三个元素重新赋值为2
minicalendar [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 1 ] 从结果来看,数组的第二个元素为数组,大于1维了,里面的元素的变更将导致原变量的值随之改变
拷贝对象,代码如下:
const time = { year: 2021, month: 7, day: { value: 1, },};const copytime = { ...time };console.log(copytime); // { year: 2021, month: 7, day: { value: 1 } }
扩展运算符拷贝对象只会在一层进行深拷贝,从下面代码是基于上面代码:
copytime.day.value = 2;copytime.month = 6;console.log(copytime); // { year: 2021, month: 6, day: { value: 2 } }console.log(time); // { year: 2021, month: 7, day: { value: 2 } }
从打印的结果看,扩展运算符只对对象第一层进行了深拷贝。
严格来讲,扩展运算符不执行深拷贝
2. 合并操作先来看数组的合并,如下:
const halfmonths1 = [1, 2, 3, 4, 5, 6];const halfmonths2 = [7, 8, 9, 10, 11, 12];const allmonths = [...halfmonths1, ...halfmonths2];console.log(allmonths); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
合并对象,在合并对象时,如果一个键已经存在,它会被具有相同键的最后一个对象给替换。
const time1 = { month: 7, day: { value: 1, },};const time2 = { year: 2021, month: 8, day: { value: 10, },};const time = { ...time1, ...time2 };console.log(time); // { month: 8, day: { value: 10 }, year: 2021 }
3. 参数传递const sum = (num1, num2) => num1 + num2;console.log(sum(...[6, 7])); // 13console.log(sum(...[6, 7, 8])); // 13
从上面的代码看,函数定义了多少个参数,扩展运算符传入的值就是多少个。
和 math 函数一起使用,如下:
const arraynumbers = [1, 5, 9, 3, 5, 7, 10];const min = math.min(...arraynumbers);const max = math.max(...arraynumbers);console.log(min); // 1console.log(max); // 10
4. 数组去重与 set 一起使用消除数组的重复项,如下:
const arraynumbers = [1, 5, 9, 3, 5, 7, 10, 4, 5, 2, 5];const newnumbers = [...new set(arraynumbers)];console.log(newnumbers); // [ 1, 5, 9, 3, 7, 10, 4, 2 ]
5. 字符串转字符数组string 也是一个可迭代对象,所以也可以使用扩展运算符 ... 将其转为字符数组,如下:
const title = "china";const charts = [...title];console.log(charts); // [ 'c', 'h', 'i', 'n', 'a' ]
进而可以简单进行字符串截取,如下:
const title = "china";const short = [...title];short.length = 2;console.log(short.join("")); // ch
6. nodelist 转数组nodelist 对象是节点的集合,通常是由属性,如 node.childnodes 和方法,如 document.queryselectorall 返回的。
nodelist 类似于数组,但不是数组,没有 array 的所有方法,例如find、map、filter 等,但是可以使用 foreach() 来迭代。
可以通过扩展运算符将其转为数组,如下:
const nodelist = document.queryselectorall(".row");const nodearray = [...nodelist];console.log(nodelist);console.log(nodearray);
7. 解构变量解构数组,如下:
const [currentmonth, ...others] = [7, 8, 9, 10, 11, 12];console.log(currentmonth); // 7console.log(others); // [ 8, 9, 10, 11, 12 ]
解构对象,如下:
const userinfo = { name: "crayon", province: "guangdong", city: "shenzhen" };const { name, ...location } = userinfo;console.log(name); // crayonconsole.log(location); // { province: 'guangdong', city: 'shenzhen' }
8. 打印日志在打印可迭代对象的时候,需要打印每一项可以使用扩展符,如下:
const years = [2018, 2019, 2020, 2021];console.log(...years); // 2018 2019 2020 2021
总结扩展运算符 … 让代码变得简洁,应该是es6中比较受欢迎的操作符了。
本文转载自:https://juejin.cn/post/6979840705921286180
更多编程相关知识,请访问:编程入门!!
以上就是了解es6扩展运算符,谈谈它的8种使用方法的详细内容。