Javascript - insert an array inside another array

前端 未结 10 2144
刺人心
刺人心 2020-12-04 09:23

What is the more efficient way to insert an array inside another array.

a1 = [1,2,3,4,5];
a2 = [21,22];

newArray - a1.insertAt(2,a2) -> [1,2, 21,22, 3,4         


        
10条回答
  •  不思量自难忘°
    2020-12-04 10:11

    The spread operator allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) are expected.

    a2 = [21,22];
    a1 = [1,2,...a2,3,4,5];//...a2 is use of spread operator
    console.log(a1);

提交回复
热议问题