JS 中使用扩展运算符的10种方法，好家伙，点个赞呗！

github 地址：https://github.com/qq44924588...

复制数组

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

合并数组

``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];``

向数组中添加元素

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

向对象添加属性

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

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

使用 Math() 函数

``const arr1 = [1, -1, 0, 5, 3];``

``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``

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

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``

向函数传递无限参数

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

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

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

将 nodeList 转换为数组

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

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

解构对象

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

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

展开字符串

``const str = 'Hello';``

``const str = 'Hello';const arr = [...str];console.log(arr);``// [ 'H', 'e', 'l', 'l', 'o' ]``

~ 完，我是小智，我要刷碗去了，我们下期再见！

交流

前端小智

55.9k 声望

9.6k 粉丝

0 条评论

github 地址：https://github.com/qq44924588...

复制数组

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

合并数组

``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];``

向数组中添加元素

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

向对象添加属性

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

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

使用 Math() 函数

``const arr1 = [1, -1, 0, 5, 3];``

``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``

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

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``

向函数传递无限参数

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

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

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

将 nodeList 转换为数组

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

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

解构对象

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

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

展开字符串

``const str = 'Hello';``

``const str = 'Hello';const arr = [...str];console.log(arr);``// [ 'H', 'e', 'l', 'l', 'o' ]``

~ 完，我是小智，我要刷碗去了，我们下期再见！