The methods of merging two arrays are:
Method one: use concat
var a = [1,2,3]; var b = [4,5,6];
var c = a.concat(b); //c=[1,2,3,4,5,6];
Method 2: Use loop
for( let i in b){ a.push(b[i]) }
Method three: use apply
a.push.apply(a,b);
in When merging arrays, you can first judge the size of the arrays. It is obviously faster to merge the larger ones with the smaller ones.
If you do not want to change the arrays, it is recommended to use the concat method.
Recommended tutorial: js introductory tutorial
The above is the detailed content of how Javascript realizes the combination of two arrays. For more, please pay attention to other related articles on 1024programmer.com!