js one array data into another array

 I encountered a problem in the project. There is a main array mainArray, and it is necessary to insert elements in multiple other small arrays. How to achieve it?

1. You can use concat:

let mainArray=[...]
let child1=[...]
let child2=[...]
let child3=[...]
 
let newArray1=mainArray.concat(child1)
let newArray2=newArray1.concat(child2)
 
...

In this method, the original array will not be modified, and a new array will be generated, but what I want is to always insert into mianArray, how to achieve it?

2. Use push

巧妙使用apply
mainArray.push.apply(mainArray,child1);
mainArray.push.apply(mainArray,child2);
...

Guess you like

Origin blog.csdn.net/qq_41954585/article/details/127064804