8種ES6中擴展運算符的用法

1、拷貝數(shù)組對象
const years = [2018, 2019, 2020, 2021];const copyYears = [...years];console.log(copyYears); // [ 2018, 2019, 2020, 2021 ]
擴展運算符拷貝數(shù)組,只有第一層是深拷貝,即對一維數(shù)組使用擴展運算符拷貝就屬于深拷貝,看下面的代碼:
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 ]
把打印的結(jié)果放在一起便于更加清楚進行對比,如下:
| 變量說明 | 結(jié)果 | 操作 |
|---|---|---|
| copyArray | [ 2021, [ 1, 2, 3, 4, 5, 6, 7 ], 1 ] | 復(fù)制數(shù)組 miniCalendar |
| copyArray | [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 2 ] | 1. 將數(shù)組第二個元素的第一個元素重新賦值為 0 ;2. 往數(shù)組的第二個元素增加一個元素 8 ;3. 將數(shù)組第三個元素重新賦值為2 |
| miniCalendar | [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 1 ] | 從結(jié)果來看,數(shù)組的第二個元素為數(shù)組,大于1維了,里面的元素的變更將導(dǎo)致原變量的值隨之改變 |
拷貝對象,代碼如下:
const time = {year: 2021,month: 7,day: {value: 1,},};const copyTime = { ...time };// { 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 } }
從打印的結(jié)果看,擴展運算符只對對象第一層進行了深拷貝。
嚴(yán)格來講,擴展運算符不執(zhí)行深拷貝
2、合并操作
先來看數(shù)組的合并,如下:
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 ]
合并對象,在合并對象時,如果一個鍵已經(jīng)存在,它會被具有相同鍵的最后一個對象給替換。
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、參數(shù)傳遞
const sum = (num1, num2) => num1 + num2;console.log(sum(...[6, 7])); // 13console.log(sum(...[6, 7, 8])); // 13
從上面的代碼看,函數(shù)定義了多少個參數(shù),擴展運算符傳入的值就是多少個。
和 math 函數(shù)一起使用,如下:
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、數(shù)組去重
與 Set 一起使用消除數(shù)組的重復(fù)項,如下:
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、字符串轉(zhuǎn)字符數(shù)組
String 也是一個可迭代對象,所以也可以使用擴展運算符 ... 將其轉(zhuǎn)為字符數(shù)組,如下:
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 轉(zhuǎn)數(shù)組
NodeList 對象是節(jié)點的集合,通常是由屬性,如 Node.childNodes 和方法,如 document.querySelectorAll 返回的。
NodeList 類似于數(shù)組,但不是數(shù)組,沒有 Array 的所有方法,例如find、map、filter 等,但是可以使用 forEach() 來迭代。
可以通過擴展運算符將其轉(zhuǎn)為數(shù)組,如下:
const nodeList = document.querySelectorAll(".row");const nodeArray = [...nodeList];console.log(nodeList);console.log(nodeArray);
7、解構(gòu)變量
解構(gòu)數(shù)組,如下:
const [currentMonth, ...others] = [7, 8, 9, 10, 11, 12];console.log(currentMonth); // 7console.log(others); // [ 8, 9, 10, 11, 12 ]
解構(gòu)對象,如下:
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
總結(jié)
擴展運算符 … 讓代碼變得簡潔,應(yīng)該是ES6中比較受歡迎的操作符了。
學(xué)習(xí)更多技能
請點擊下方公眾號
![]()

