JS array merge (one array added to another array) method

Source: Internet
Author: User

JS defines two arrays.

var arra=[1,2,3];

var arrb=[4,5,6];

To achieve [1,2,3,4,5,6], if direct arra.push (ARRB); Then ARRB will only act as an element of the ARRA. Perform

To merge or connect, you need to use the concat () method.

Concat (Array) method

The Concat () method is used to concatenate two or more arrays. The method does not alter the existing array, but only returns a copy of the concatenated array.
Array1.concat ([item1[, item2[, ... [, Itemn]]])
Description
The Concat method returns an Array object that contains the connection for array1 and any other items provided.
Items to add (item1 ... itemn) are added to the array in left-to-right order. If an item is an array, then add its contents to the end of the array1. If the item is not an array, it is added to the end of the array as a single array element.
The following is a copy of the element from the source array to the result array:
For the object parameters copied from the array being connected to the new array, the same object is still pointed to after replication. Any change in the new array and the source array will cause another change.
For numeric values or strings connected to a new array, only its value is copied. Changing the value in one array does not affect the value in the other array.

// concat () links two or more arrays together, but does not change existing arrays // Instead, it returns a new array after a link var a = [];a.concat ([4,5]); Console.log (a); // output here is [1, 2, 3] var a = [a.concat ([4,5]); Console.log (a); // output here is [1, 2, 3, 4, 5]

JS array merge (one array added to another array) method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.