JavaScript analog array Merging concat_javascript techniques

Source: Internet
Author: User

Definitions and usage

The Concat () method is used to connect two or more arrays.

This method does not change an existing array, but merely returns a copy of the connected array.

Grammar

Arrayobject.concat (Arrayx,arrayx,......, Arrayx)

Parameters Description
Arrayx Necessary. The parameter can be a specific value, or it can be an array object. Can be any number of.
return value

Returns a new array. The array is generated by adding all the Arrayx parameters to the Arrayobject. If the argument for the concat () operation is an array, then the element in the array is added, not the array

We have two of these arrays

var arr1 = [1,2,3];
var arr2 = [4,5,6];

Task: Merge into this, please provide at least two kinds of ideas.

var arr1 = [1,2,3,4,5,6];

thought one: We can add the values in the second array to the end of the first array.

1: How does an array add content? [] || Push | | Shift

2: How do I add a value to the last index of an array? Push | | [Array. length]

3: How do I add content to an array? For

How many times does the 4:for cycle? You want to add how many times, that is, the length of arr2

5: What do you want to add? ARR2 inside the value, how to obtain, arr[?]

Code implementation:

var arr1 = [1,2,3];
var arr2 = [4,5,6];
for (Var i=0;i<arr2.length;i++) {
arr1.push (arr2[i]);
}
Console.log (ARR1); [1, 2, 3, 4, 5, 6]

The problem is that the concat method provided by native JS does not modify the contents of the original array (ARR1), but instead returns a new array.

Analysis: Since it is to return a value, then we can define a function, and then define a variable, this variable to receive we add a good value, but we will be a problem, that is not arr1 above the push content, or will modify the contents of the original array. So I thought we were going to copy the original array, but again, the problem is that the object is a reference type, and in a nutshell, although we can copy the array 1 to a variable, if I modify the value in the push or [], or add, then our original array will be modified, (If you don't know what the reference type is, you can go to the first page of my blog or on the second page) this is not the result we want, but we have to copy a arr1. What is your solution at this time?

To resolve an array reference problem:

for (Var i=0;i<arr1.length;i++) {
arr3[i] = Arr1[i];
}

My idea is to add the value of array 1 to the ARR3 array, which is the ARR3

Console.log (ARR3)//[1, 2, 3]

Task: Add the ARR2 value to this new array Arr3 method, as in the first step, if you forget to look back.

Code implementation:

var arr1 = [1,2,3];
var arr2 = [4,5,6];
var arr3 = [];
for (Var i=0;i<arr1.length;i++) {
arr3[i] = Arr1[i];
}
for (Var i=0;i<arr2.length;i++) {
arr3.push (arr2[i]);
}
Console.log (ARR3);

Problem: Although this has implemented the merging of arrays, but I have to write a new one each time, it is too cumbersome, so we have to think of a way to encapsulate it into a function, the next time we need to call it on the line.

var arr1 = [1,2,3];
var arr2 = [4,5,6,7];
function Concat (ARR1,ARR2) {
var arr3 = [];
for (Var i=0;i<arr1.length;i++) {
arr3[i] = Arr1[i];
}
for (Var i=0;i<arr2.length;i++) {
arr3.push (arr2[i]);
}
return ARR3;
}
Console.log (Concat (ARR1,ARR2));

Idea two:

Analysis: Convert arr1 and ARR2 to strings, then add the two strings together to get a combination and then the string into a group.

Code implementation:

var arr1 = [1,2,3];
var arr2 = [4,5,6,7,8,9];
var arr3 = (Arr1.join (",") + "," +arr2.join (",")). Split (",");

A little bit of a problem, the value in this array is a string.

["1", "2", "3", "4", "5", "6", "7", "8", "9"]

Solution: Iterate through the array and convert them into numbers.

var arr1 = [1,2,3];
var arr2 = [4,5,6,7,8,9];
var arr3 = (Arr1.join (",") + "," +arr2.join (",")). Split (",");
for (Var i=0;i<arr3.length;i++) {
arr3[i] = +arr3[i];
}
Console.log (ARR3);

Extracurricular extensions: Inheriting version

var arr1 = [1,2,3];
var arr2 = [4,5,6,7,8,9];
Array.prototype.Concat = function (arr) {
var arr3 = [];
for (Var i=0;i<this.length;i++) {
arr3[i] = this[i];
}
for (Var i=0;i<arr.length;i++) {
arr3.push (arr[i]);
}
return ARR3;
}
Console.log (arr1. Concat (ARR2));

The above is a small set up to introduce the JavaScript analog array of the integration of concat knowledge, hope to help you!

Related Article

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.