Using es6 spread to concat multiple arrays

前端 未结 8 1246
伪装坚强ぢ
伪装坚强ぢ 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:15

    You can use spread element within for..of loop to concatenate array values to a single array

    let arr1 = [1,2,3];
    let arr2 = [3,4,5];
    let arr3 = [];
    
    for (let arr of [arr1, arr2 /* , arrN */]) arr3.push(...arr);
    
    console.log(arr3);

提交回复
热议问题