Using es6 spread to concat multiple arrays

前端 未结 8 1242
伪装坚强ぢ
伪装坚强ぢ 2021-01-31 14:50

We all know you can do:

let arr1 = [1,2,3];
let arr2 = [3,4,5];
let arr3 = [...arr1, ...arr2]; // [1,2,3,3,4,5]

But how do you make this dynami

8条回答
  •  有刺的猬
    2021-01-31 15:26

    Following solution works for me (spread operator in ES6):

    let array = ['my','solution','works'];
    let newArray = [];
    let newArray2 = [];
    newArray.push(...array); //adding to same array
    newArray2.push([...array]); //adding as child/leaf/sub-array
    console.log(newArray);
    console.log(newArray2);

提交回复
热议问题