擴展操作符 … 是ES6中引入的,將可迭代對象展開到其單獨的元素中,所謂的可迭代對象就是任何能用for of循環進行遍歷的對象,例如:數組(數組常用方法)、字符串、Map (悟透Map)、Set (Set 如何使用?)、DOM節點等。
- 拷貝數組對象
使用擴展符拷貝數組是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 } }
從打印的結果看,擴展運算符只對對象第一層進行了深拷貝。
嚴格來講,擴展運算符不執行深拷貝
- 合並操作
先來看數組的合並,如下:
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])); // 13
console.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); // 1
console.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);
- 解構變量
解構數組,如下:
const [currentMonth, ...others] = [7, 8, 9, 10, 11, 12];
console.log(currentMonth); // 7
console.log(others); // [ 8, 9, 10, 11, 12 ]
解構對象,如下:
const userInfo = { name: "Crayon", province: "Guangdong", city: "Shenzhen" };
const { name, ...location } = userInfo;
console.log(name); // Crayon
console.log(location); // { province: 'Guangdong', city: 'Shenzhen' }
8. 打印日志
在打印可迭代對象的時候,需要打印每一項可以使用擴展符,如下:
const years = [2018, 2019, 2020, 2021];
console.log(...years); // 2018 2019 2020 2021
總結
擴展運算符 … 讓代碼變得簡潔,應該是ES6中比較受歡迎的操作符了。