How to merge arrays using JavaScript
This article mainly introduces how to use JavaScript to merge arrays, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Merge array
If you want to merge two arrays, you will normally use the Array.concat () function:
Var array1 = [1Jing 2 Jue 3]; var array2 = [4 Jet 5 Jol 6]; console.log (array1.concat (array2)); / / [1 Jing 2 Jing 3 Jing 4 Ji 5 Ji 6]
Then this function is not suitable for merging two large arrays because it consumes a lot of memory to store the newly created array. In this case, you can use Array.pus (). Apply (arr1,arr2) instead of creating a new array. This method is not used to create a new array, it simply combines * second numbers together, while reducing memory usage:
Var array1 = [1Jing 2jue 3]; var array2 = [4je 5je 6]; console.log (array1.push.apply (array1, array2)); / / [1Jing 2Jing 3Jing 4je 5je 6]. Thank you for reading this article carefully. I hope the article "how to use JavaScript to merge arrays" shared by the editor will be helpful to you. At the same time, I also hope you can support us, pay attention to the industry information channel, and more related knowledge is waiting for you to learn!