Get the App
SLTechnology News&Howtos  ›  Development  › 

How JavaScript merges arrays

Shulou Source: shulou.com Published: 2022-06-01 04:21:33 09月22日 Update

This article is about how JavaScript merges arrays. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Merge array

If you need to merge two arrays, you can 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]

However, this function is not appropriate for large arrays because it will create a new array and consume a lot of memory. In this case, you can use Array.push.apply (arr1,arr2), which does not create a new array, but merges the second array into the first array to reduce memory usage:

Var array1 = [1Jing 2jue 3]; var array2 = [4je 5je 6]; console.log (array1.push.apply (array1, array2)); / / [1je 2je 3je 4je 5je 6]. Thank you for reading! This is the end of the article on "how to merge arrays with JavaScript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Array memory content function more article good appropriate practical two number will create situation article see knowledge but reference help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Linux MySQL Huawei NVidia