溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

es6中擴展運算符如何用

發(fā)布時間:2022-10-12 10:05:15 來源:億速云 閱讀:301 作者:iii 欄目:web開發(fā)

本篇內容介紹了“es6中擴展運算符如何用”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

es6擴展運算符的用法:1、復制數(shù)組,語法“[...數(shù)組]”;2、合并數(shù)組,語法“[...數(shù)組1, ...數(shù)組2]”;3、向數(shù)組中添加元素,語法“[...數(shù)組, '元素值']”;4、和Math對象一起使用,計算最大值、最小值或者總和;5、向函數(shù)傳遞無限參數(shù),語法“const myFunc=(...args)=>{};”;6、將字符串轉字符數(shù)組,語法“[...字符串]”。

本教程操作環(huán)境:windows7系統(tǒng)、ECMAScript 6版、Dell G3電腦。

es6中擴展運算符介紹

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

它好比 rest 參數(shù)的逆運算,將一個數(shù)組轉為用逗號分隔的參數(shù)序列。擴展運算符與正常的函數(shù)參數(shù)可以結合使用,后面也可以放置表達式,但如果后面是一個空數(shù)組,則不產生任何效果。

let arr = [];
arr.push(...[1,2,3,4,5]);
console.log(arr); //[1,2,3,4,5]
console.log(1, ...[2, 3, 4], 5) //1 2 3 4 5
console.log(...(1 > 0 ? ['a'] : [])); //a
console.log([...[], 1]); //[1]

意義

替代函數(shù)的apply方法

由于擴展運算符可以展開數(shù)組,所以不再需要apply方法,將數(shù)組轉為函數(shù)的參數(shù)了。

擴展運算符(...)的10+種用法

1、復制數(shù)組

我們可以使用展開操作符復制數(shù)組,不過要注意的是這是一個淺拷貝

const arr1 = [1,2,3];
const arr2 = [...arr1];
console.log(arr2);
// [ 1, 2, 3 ]

這樣我們就可以復制一個基本的數(shù)組,注意,它不適用于多級數(shù)組或帶有日期或函數(shù)的數(shù)組。

2、合并數(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 ]

我們還可以通過不同的排列方式來說明哪個應該先出現(xiàn)。

const arr3 = [...arr2, ...arr1];
console.log(arr3);
[4, 5, 6, 1, 2, 3];

此外,展開運算符號還適用多個數(shù)組的合并:

const output = [...arr1, ...arr2, ...arr3, ...arr4];

3、向數(shù)組中添加元素

let arr1 = ['this', 'is', 'an'];
arr1 = [...arr1, 'array'];
console.log(arr1);
// [ 'this', 'is', 'an', 'array' ]

4、向對象添加屬性

假設你有一個user 的對象,但它缺少一個age屬性。

const user = {
  firstname: 'Chris',
  lastname: 'Bongers'
};

要向這個user對象添加age,我們可以再次利用展開操作符。

const output = {...user, age: 31};

5、使用 Math() 函數(shù)

假設我們有一個數(shù)字數(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,因為JavaScript不知道數(shù)組的最大值是什么。

6、rest 參數(shù)

假設我們有一個函數(shù),它有三個參數(shù)。

const myFunc(x1, x2, x3) => {
    console.log(x1);
    console.log(x2);
    console.log(x3);
}

我們可以按以下方式調用這個函數(shù):

myFunc(1, 2, 3);

但是,如果我們要傳遞一個數(shù)組會發(fā)生什么。

const arr1 = [1, 2, 3];

我們可以使用展開操作符將這個數(shù)組擴展到我們的函數(shù)中。

myFunc(...arr1);
// 1
// 2
// 3

這里,我們將數(shù)組分為三個單獨的參數(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

7、向函數(shù)傳遞無限參數(shù)

假設我們有一個函數(shù),它接受無限個參數(shù),如下所示:

const myFunc = (...args) => {
  console.log(args);
};

如果我們現(xiàn)在調用這個帶有多個參數(shù)的函數(shù),會看到下面的情況:

myFunc(1, 'a', new Date());

返回:

[
  1,
  'a',
  Date {
    __proto__: Date {}
  }
]

然后,我們就可以動態(tài)地循環(huán)遍歷參數(shù)。

8、將 nodeList 轉換為數(shù)組

假設我們使用了展開運算符來獲取頁面上的所有p

const el = [...document.querySelectorAll('p')];
console.log(el);
// (3) [p, p, p]

在這里可以看到我們從dom中獲得了3個p。

現(xiàn)在,我們可以輕松地遍歷這些元素,因為它們是數(shù)組了。

const el = [...document.querySelectorAll('p')];
el.forEach(item => {
  console.log(item);
});
// <p></p>
// <p></p>
// <p></p>

9、解構變量

解構對象

假設我們有一個對象user:

const user = {
  firstname: 'Chris',
  lastname: 'Bongers',
  age: 31
};

現(xiàn)在,我們可以使用展開運算符將其分解為單個變量。

const {firstname, ...rest} = user;
console.log(firstname);
console.log(rest);
// 'Chris'
// { lastname: 'Bongers', age: 31 }

這里,我們解構了user對象,并將firstname解構為firstname變量,將對象的其余部分解構為rest變量。

解構數(shù)組

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

10、展開字符串(字符串轉字符數(shù)組)

String 也是一個可迭代對象,所以也可以使用擴展運算符 ... 將其轉為字符數(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

11、數(shù)組去重

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

const arrayNumbers = [1, 5, 9, 3, 5, 7, 10, 4, 5, 2, 5];
console.log(arrayNumbers);
const newNumbers = [...new Set(arrayNumbers)];
console.log(newNumbers); // [ 1,  5, 9, 3, 7, 10, 4, 2 ]

登錄后復制

es6中擴展運算符如何用

12、打印日志

在打印可迭代對象的時候,需要打印每一項可以使用擴展符,如下:

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

“es6中擴展運算符如何用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

es6
AI