我是靠谱客的博主 老实大门,这篇文章主要介绍es6中扩展运算符怎么用,现在分享给大家,希望可以做个参考。

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

es6中扩展运算符介绍

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

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

复制代码
1
2
3
4
5
6
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]
登录后复制

意义

替代函数的apply方法

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

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

1、复制数组

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

复制代码
1
2
3
4
const arr1 = [1,2,3]; const arr2 = [...arr1]; console.log(arr2); // [ 1, 2, 3 ]
登录后复制

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

2、合并数组

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

复制代码
1
2
3
4
5
const arr1 = [1,2,3]; const arr2 = [4,5,6]; const arr3 = [...arr1, ...arr2]; console.log(arr3); // [ 1, 2, 3, 4, 5, 6 ]
登录后复制

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

复制代码
1
2
3
const arr3 = [...arr2, ...arr1]; console.log(arr3); [4, 5, 6, 1, 2, 3];
登录后复制

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

复制代码
1
const output = [...arr1, ...arr2, ...arr3, ...arr4];
登录后复制

3、向数组中添加元素

复制代码
1
2
3
4
let arr1 = ['this', 'is', 'an']; arr1 = [...arr1, 'array']; console.log(arr1); // [ 'this', 'is', 'an', 'array' ]
登录后复制

4、向对象添加属性

假设你有一个user 的对象,但它缺少一个age属性。

复制代码
1
2
3
4
const user = { firstname: 'Chris', lastname: 'Bongers' };
登录后复制

要向这个user对象添加age,我们可以再次利用展开操作符。

复制代码
1
const output = {...user, age: 31};
登录后复制

5、使用 Math() 函数

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

复制代码
1
const arr1 = [1, -1, 0, 5, 3];
登录后复制

为了获得最小值,我们可以使用展开操作符和 Math.min 方法。

复制代码
1
2
3
4
const arr1 = [1, -1, 0, 5, 3]; const min = Math.min(...arr1); console.log(min); // -1
登录后复制

同样,要获得最大值,可以这么做:

复制代码
1
2
3
4
const arr1 = [1, -1, 0, 5, 3]; const max = Math.max(...arr1); console.log(max); // 5
登录后复制

如大家所见,最大值5,如果我们删除5,它将返回3

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

复制代码
1
2
3
4
const arr1 = [1, -1, 0, 5, 3]; const max = Math.max(arr1); console.log(max); // NaN
登录后复制

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

6、rest 参数

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

复制代码
1
2
3
4
5
const myFunc(x1, x2, x3) => { console.log(x1); console.log(x2); console.log(x3); }
登录后复制

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

复制代码
1
myFunc(1, 2, 3);
登录后复制

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

复制代码
1
const arr1 = [1, 2, 3];
登录后复制

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

复制代码
1
2
3
4
myFunc(...arr1); // 1 // 2 // 3
登录后复制

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

复制代码
1
2
3
4
5
6
7
8
9
10
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、向函数传递无限参数

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

复制代码
1
2
3
const myFunc = (...args) => { console.log(args); };
登录后复制

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

复制代码
1
myFunc(1, 'a', new Date());
登录后复制

返回:

复制代码
1
2
3
4
5
6
7
[ 1, 'a', Date { __proto__: Date {} } ]
登录后复制

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

8、将 nodeList 转换为数组

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

复制代码
1
2
3
const el = [...document.querySelectorAll('p')]; console.log(el); // (3) [p, p, p]
登录后复制

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

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

复制代码
1
2
3
4
5
6
7
const el = [...document.querySelectorAll('p')]; el.forEach(item => { console.log(item); }); // <p></p> // <p></p> // <p></p>
登录后复制

9、解构变量

解构对象

假设我们有一个对象user:

复制代码
1
2
3
4
5
const user = { firstname: 'Chris', lastname: 'Bongers', age: 31 };
登录后复制

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

复制代码
1
2
3
4
5
const {firstname, ...rest} = user; console.log(firstname); console.log(rest); // 'Chris' // { lastname: 'Bongers', age: 31 }
登录后复制

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

解构数组

复制代码
1
2
3
const [currentMonth, ...others] = [7, 8, 9, 10, 11, 12]; console.log(currentMonth); // 7 console.log(others); // [ 8, 9, 10, 11, 12 ]
登录后复制

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

String 也是一个可迭代对象,所以也可以使用扩展运算符 ... 将其转为字符数组,如下:

复制代码
1
2
3
const title = "china"; const charts = [...title]; console.log(charts); // [ 'c', 'h', 'i', 'n', 'a' ]
登录后复制

进而可以简单进行字符串截取,如下:

复制代码
1
2
3
4
const title = "china"; const short = [...title]; short.length = 2; console.log(short.join("")); // ch
登录后复制

11、数组去重

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

复制代码
1
2
3
4
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 ]
登录后复制

1.png

12、打印日志

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

复制代码
1
2
const years = [2018, 2019, 2020, 2021]; console.log(...years); // 2018 2019 2020 2021
登录后复制

【相关推荐:web前端开发】

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

最后

以上就是老实大门最近收集整理的关于es6中扩展运算符怎么用的全部内容,更多相关es6中扩展运算符怎么用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部