聊聊ES6擴(kuò)展運(yùn)算符“…”的8種用法(轉(zhuǎn)載)

image

擴(kuò)展操作符 是ES6中引入的,將可迭代對象展開到其單獨(dú)的元素中,所謂的可迭代對象就是任何能用for of循環(huán)進(jìn)行遍歷的對象,例如:數(shù)組字符串、Map 、Set 、DOM節(jié)點(diǎn)等。

1. 拷貝數(shù)組對象

使用擴(kuò)展符拷貝數(shù)組是ES6中常用的操作:

const years = [2018, 2019, 2020, 2021];
const copyYears = [...years];

console.log(copyYears); // [ 2018, 2019, 2020, 2021 ]

擴(kuò)展運(yùn)算符拷貝數(shù)組,只有第一層是深拷貝,即對一維數(shù)組使用擴(kuò)展運(yùn)算符拷貝就屬于深拷貝,看下面的代碼:

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é)果放在一起便于更加清楚進(jìn)行對比,如下:

變量說明 結(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ù)組第二個(gè)元素的第一個(gè)元素重新賦值為 0 ;2. 往數(shù)組的第二個(gè)元素增加一個(gè)元素 8 ;3. 將數(shù)組第三個(gè)元素重新賦值為2
miniCalendar [ 2021, [ 0, 2, 3, 4, 5, 6, 7, 8 ], 1 ] 從結(jié)果來看,數(shù)組的第二個(gè)元素為數(shù)組,大于1維了,里面的元素的變更將導(dǎo)致原變量的值隨之改變

拷貝對象,代碼如下:

const time = {
    year: 2021,
    month: 7,
    day: {
        value: 1,
    },
};
const copyTime = { ...time };
console.log(copyTime); // { year: 2021, month: 7, day: { value: 1 } }

擴(kuò)展運(yùn)算符拷貝對象只會在一層進(jìn)行深拷貝,從下面代碼是基于上面代碼:

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é)果看,擴(kuò)展運(yùn)算符只對對象第一層進(jìn)行了深拷貝。

嚴(yán)格來講,擴(kuò)展運(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 ]

合并對象,在合并對象時(shí),如果一個(gè)鍵已經(jīng)存在,它會被具有相同鍵的最后一個(gè)對象給替換。

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])); // 13
console.log(sum(...[6, 7, 8])); // 13

從上面的代碼看,函數(shù)定義了多少個(gè)參數(shù),擴(kuò)展運(yùn)算符傳入的值就是多少個(gè)。

math 函數(shù)一起使用,如下:

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. 數(shù)組去重

Set 一起使用消除數(shù)組的重復(fù)項(xiàng),如下:

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 也是一個(gè)可迭代對象,所以也可以使用擴(kuò)展運(yùn)算符 ... 將其轉(zhuǎn)為字符數(shù)組,如下:

const title = "china";
const charts = [...title];
console.log(charts); // [ 'c', 'h', 'i', 'n', 'a' ]

進(jìn)而可以簡單進(jìn)行字符串截取,如下:

const title = "china";
const short = [...title];
short.length = 2;
console.log(short.join("")); // ch

6. NodeList 轉(zhuǎn)數(shù)組

NodeList 對象是節(jié)點(diǎn)的集合,通常是由屬性,如 Node.childNodes 和方法,如 document.querySelectorAll 返回的。

NodeList 類似于數(shù)組,但不是數(shù)組,沒有 Array 的所有方法,例如findmap、filter 等,但是可以使用 forEach() 來迭代。

可以通過擴(kuò)展運(yùn)算符將其轉(zhuǎn)為數(shù)組,如下:

const nodeList = document.querySelectorAll(".row");
const nodeArray = [...nodeList];
console.log(nodeList);
console.log(nodeArray);
1.jpg

7. 解構(gòu)變量

解構(gòu)數(shù)組,如下:

const [currentMonth, ...others] = [7, 8, 9, 10, 11, 12];
console.log(currentMonth); // 7
console.log(others); // [ 8, 9, 10, 11, 12 ]

解構(gòu)對象,如下:

const userInfo = { name: "Crayon", province: "Guangdong", city: "Shenzhen" };
const { name, ...location } = userInfo;
console.log(name); // Crayon
console.log(location); // { province: 'Guangdong', city: 'Shenzhen' }

8. 打印日志

在打印可迭代對象的時(shí)候,需要打印每一項(xiàng)可以使用擴(kuò)展符,如下:

const years = [2018, 2019, 2020, 2021];
console.log(...years); // 2018 2019 2020 2021

總結(jié)

擴(kuò)展運(yùn)算符 讓代碼變得簡潔,應(yīng)該是ES6中比較受歡迎的操作符了。

原文地址:https://juejin.cn/post/6979840705921286180

本文轉(zhuǎn)載自:https://www.php.cn/js-tutorial-479781.html

更多編程相關(guān)知識,請?jiān)L問:編程入門??!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容