欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

JavaScript中的扩展运算符

程序员文章站 2022-04-11 11:56:00
...

扩展运算符(Spread operator)

let zzz=[2,4,6];
console.log(zzz);//[2, 4, 6]
console.log(...zzz);//2 4 6

展开并合并

let a=[1,2,3];
let b=[...a,4,5,6];
console.log(b);//1,2,3,4,5,6

let da=[1,2];
let db=Array.from(da);
db.push(3);
console.log(da,db);//[1, 2]   [1, 2, 3]

concat的连接数组

var arr5= [3,4,5]
    var arr6 = [6,7,8]
    var arr7 = arr5.concat(arr6);
    console.log(arr7);//3,4,5,6,7,8
    var arr8 = [...arr5,...arr6];
    console.log(arr8);//3,4,5,6,7,8

将字符串转化成数组

var str = 'html';
var arr9 = [...str];
console.log(arr9);//["h", "t", "m", "l"]

应用在解构赋值里

let [a,b,...c]=[1,2,3,4,5];
console.log(a,b);//1 2
console.log(c);//[3, 4, 5]

var aa=[1,2,3];
var bb=[...aa];
bb.push(4,5);
console.log(bb);//[1, 2, 3, 4, 5]

函数调用

function aa(a,b){
        return a+b
}
let cc=[1,3]
let bb=aa(...cc)
console.log(bb);//4

以上就是JavaScript中的扩展运算符的详细内容,更多请关注其它相关文章!