十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要介紹javascript中spread運(yùn)算符怎么用,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供馬村網(wǎng)站建設(shè)、馬村做網(wǎng)站、馬村網(wǎng)站設(shè)計、馬村網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、馬村企業(yè)網(wǎng)站模板建站服務(wù),十年馬村做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
我們可以使用展開操作符復(fù)制數(shù)組,不過要注意的是這是一個淺拷貝。
const arr1 = [1,2,3]; const arr2 = [...arr1]; console.log(arr2); // [ 1, 2, 3 ]
這樣我們就可以復(fù)制一個基本的數(shù)組,注意,它不適用于多級數(shù)組或帶有日期或函數(shù)的數(shù)組。
假設(shè)我們有兩個數(shù)組想合并為一個,早期間我們可以使用concat方法,但現(xiàn)在可以使用展開操作符:
const arr1 = [1,2,3]; const arr2 = [4,5,6]; const arr3 = [...arr1, ...arr2]; console.log(arr3); // [ 1, 2, 3, 4, 5, 6 ]
我們還可以通過不同的排列方式來說明哪個應(yīng)該先出現(xiàn)。
const arr3 = [...arr2, ...arr1]; console.log(arr3); [4, 5, 6, 1, 2, 3];
此外,展開運(yùn)算符號還適用多個數(shù)組的合并:
const output = [...arr1, ...arr2, ...arr3, ...arr4];
let arr1 = ['this', 'is', 'an']; arr1 = [...arr1, 'array']; console.log(arr1); // [ 'this', 'is', 'an', 'array' ]
假設(shè)你有一個user 的對象,但它缺少一個age屬性。
const user = {
firstname: 'Chris',
lastname: 'Bongers'
};要向這個user對象添加age,我們可以再次利用展開操作符。
const output = {...user, age: 31};假設(shè)我們有一個數(shù)字?jǐn)?shù)組,我們想要獲得這些數(shù)字中的最大值、最小值或者總和。
const arr1 = [1, -1, 0, 5, 3];
為了獲得最小值,我們可以使用展開操作符和 Math.min 方法。
const arr1 = [1, -1, 0, 5, 3]; const min = Math.min(...arr1); console.log(min); // -1
同樣,要獲得最大值,可以這么做:
const arr1 = [1, -1, 0, 5, 3]; const max = Math.max(...arr1); console.log(max); // 5
如大家所見,最大值5,如果我們刪除5,它將返回3。
你可能會好奇,如果我們不使用展開操作符會發(fā)生什么?
const arr1 = [1, -1, 0, 5, 3]; const max = Math.max(arr1); console.log(max); // NaN
這會返回NaN,因?yàn)镴avaScript不知道數(shù)組的最大值是什么。
假設(shè)我們有一個函數(shù),它有三個參數(shù)。
const myFunc(x1, x2, x3) => {
console.log(x1);
console.log(x2);
console.log(x3);
}我們可以按以下方式調(diào)用這個函數(shù):
myFunc(1, 2, 3);
但是,如果我們要傳遞一個數(shù)組會發(fā)生什么。
const arr1 = [1, 2, 3];
我們可以使用展開操作符將這個數(shù)組擴(kuò)展到我們的函數(shù)中。
myFunc(...arr1); // 1 // 2 // 3
這里,我們將數(shù)組分為三個單獨(dú)的參數(shù),然后傳遞給函數(shù)。
const myFunc = (x1, x2, x3) => {
console.log(x1);
console.log(x2);
console.log(x3);
};
const arr1 = [1, 2, 3];
myFunc(...arr1);
// 1
// 2
// 3假設(shè)我們有一個函數(shù),它接受無限個參數(shù),如下所示:
const myFunc = (...args) => {
console.log(args);
};如果我們現(xiàn)在調(diào)用這個帶有多個參數(shù)的函數(shù),會看到下面的情況:
myFunc(1, 'a', new Date());
返回:
[
1,
'a',
Date {
__proto__: Date {}
}
]然后,我們就可以動態(tài)地循環(huán)遍歷參數(shù)。
假設(shè)我們使用了展開運(yùn)算符來獲取頁面上的所有p:
const el = [...document.querySelectorAll('p')];
console.log(el);
// (3) [p, p, p]在這里可以看到我們從dom中獲得了3個p。
現(xiàn)在,我們可以輕松地遍歷這些元素,因?yàn)樗鼈兪菙?shù)組了。
const el = [...document.querySelectorAll('p')];
el.forEach(item => {
console.log(item);
});
//
//
// 假設(shè)我們有一個對象user:
const user = {
firstname: 'Chris',
lastname: 'Bongers',
age: 31
};現(xiàn)在,我們可以使用展開運(yùn)算符將其分解為單個變量。
const {firstname, ...rest} = user;
console.log(firstname);
console.log(rest);
// 'Chris'
// { lastname: 'Bongers', age: 31 }這里,我們解構(gòu)了user對象,并將firstname解構(gòu)為firstname變量,將對象的其余部分解構(gòu)為rest變量。
展開運(yùn)算符的最后一個用例是將一個字符串分解成單個單詞。
假設(shè)我們有以下字符串:
const str = 'Hello';
然后,如果我們對這個字符串使用展開操作符,我們將得到一個字母數(shù)組。
const str = 'Hello'; const arr = [...str]; console.log(arr); // [ 'H', 'e', 'l', 'l', 'o' ]
以上是“javascript中spread運(yùn)算符怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!