es6中扩展运算符怎么用-天天微头条

来源:php中文网 | 2022-10-11 17:45:00 |

前端(vue)入门到精通课程:进入学习API 文档、设计、调试、自动化测试一体化协作工具:点击使用

本教程操作环境:windows7系统、ECMAScript 6版、Dell G3电脑。


(资料图片)

es6中扩展运算符介绍

扩展操作符 … 是ES6中引入的,将可迭代对象展开到其单独的元素中,所谓的可迭代对象就是任何能用for of循环进行遍历的对象,例如:数组(数组常用方法)、字符串、Map (悟透Map)、Set (Set 如何使用?)、DOM节点等。

它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。扩展运算符与正常的函数参数可以结合使用,后面也可以放置表达式,但如果后面是一个空数组,则不产生任何效果。

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 5console.log(...(1 > 0 ? ["a"] : [])); //aconsole.log([...[], 1]); //[1]

意义

替代函数的apply方法

由于扩展运算符可以展开数组,所以不再需要apply方法,将数组转为函数的参数了。

扩展运算符(...)的10+种用法

1、复制数组

我们可以使用展开操作符复制数组,不过要注意的是这是一个浅拷贝

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

这样我们就可以复制一个基本的数组,注意,它不适用于多级数组或带有日期或函数的数组。

2、合并数组

假设我们有两个数组想合并为一个,早期间我们可以使用concat方法,但现在可以使用展开操作符:

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

我们还可以通过不同的排列方式来说明哪个应该先出现。

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

此外,展开运算符号还适用多个数组的合并:

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

3、向数组中添加元素

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() 函数

假设我们有一个数字数组,我们想要获得这些数字中的最大值、最小值或者总和。

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

你可能会好奇,如果我们不使用展开操作符会发生什么?

const arr1 = [1, -1, 0, 5, 3];const max = Math.max(arr1);console.log(max);// NaN

这会返回NaN,因为JavaScript不知道数组的最大值是什么。

6、rest 参数

假设我们有一个函数,它有三个参数。

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

我们可以按以下方式调用这个函数:

myFunc(1, 2, 3);

但是,如果我们要传递一个数组会发生什么。

const arr1 = [1, 2, 3];

我们可以使用展开操作符将这个数组扩展到我们的函数中。

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

这里,我们将数组分为三个单独的参数,然后传递给函数。

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、向函数传递无限参数

假设我们有一个函数,它接受无限个参数,如下所示:

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

如果我们现在调用这个带有多个参数的函数,会看到下面的情况:

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

返回:

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

然后,我们就可以动态地循环遍历参数。

8、将 nodeList 转换为数组

假设我们使用了展开运算符来获取页面上的所有p

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

在这里可以看到我们从dom中获得了3个p

现在,我们可以轻松地遍历这些元素,因为它们是数组了。

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};

现在,我们可以使用展开运算符将其分解为单个变量。

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

这里,我们解构了user对象,并将firstname解构为firstname变量,将对象的其余部分解构为rest变量。

解构数组

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

10、展开字符串(字符串转字符数组)

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

11、数组去重

与 Set 一起使用消除数组的重复项,如下:

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 ]

12、打印日志

在打印可迭代对象的时候,需要打印每一项可以使用扩展符,如下:

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

以上就是es6中扩展运算符怎么用的详细内容,更多请关注php中文网其它相关文章!

关键词: javascript es6字符串 es6数组