JavaScript simulated array merging concat and javascriptconcat
Definition and usage
The concat () method is used to connect two or more arrays.
This method does not change the existing array, but only returns a copy of the connected array.
Syntax
arrayObject.concat(arrayX,arrayX,......,arrayX)
Parameters |
Description |
ArrayX |
Required. This parameter can be a specific value or an array object. It can be any number. |
Return Value
Returns a new array. This array is generated by adding all arrayX parameters to arrayObject. If the concat () operation parameter is an array, the elements in the array are added instead of the array.
.
We have two such arrays.
var arr1 = [1,2,3];var arr2 = [4,5,6];
Task: Merge the tasks in this way. Please provide at least two ideas.
var arr1 = [1,2,3,4,5,6];
Thought 1:We can add the values in the second number group one by one to the end of the first array.
1: how to add content to an array? [] | Push | shift
2: how to add a value to the last index of the array? Push | [array. length]
3: how to add content to an array one by one? For
4: How many for loops? The number of times you want to add a loop, that is, the length of arr2.
5: What content should I add? How to obtain the value in arr2? 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 content of the original array (arr1), but returns a new array.
Analysis: to return a value, we can define a function and a variable to receive the added value, but we will have a problem, that is, the content cannot be pushed on arr1. Otherwise, the content of the original array will be modified. So I think we need to copy the original array, but there is another problem, that is, the object is a reference type. In short, although we can copy array 1 to a variable, however, if I use push or [] to modify or add values, our original array will also be modified. (If you do not know what the reference type is, you can check the first or second page of my blog.) This is not the expected result, but we must copy an arr1. What are your solutions?
Solve the 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 array arr3 one by one. At this time, arr3 is like this.
console.log(arr3) //[1, 2, 3]
Task: add the value of arr2 to the new array arr3. The method is the same as the first step.
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 array merging has been implemented, it is too troublesome for me to re-write each merge, so we have to try to encapsulate it into a function, you can call it next time.
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));
Thought 2:
Analysis: Convert both arr1 and arr2 into strings, then add the two strings to a combination, and then convert the strings into arrays.
Code implementation:
var arr1 = [1,2,3];var arr2 = [4,5,6,7,8,9];var arr3 = (arr1.join(",")+","+arr2.join(",")).split(",");
There is a small problem. The value in this array is a string.
["1", "2", "3", "4", "5", "6", "7", "8", "9"]
Solution: traverse the array and convert them to numbers one by one.
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);
Extra-curricular Extension: inherited Edition
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 Editor to introduce you to the JavaScript Analog Array merging concat knowledge, I hope to help you!
Articles you may be interested in:
- JS implements the method of merging two arrays and removing duplicate items and leaving only one
- Difference between JS array merge push and concat
- Examples of array merging and sorting in JavaScript
- JavaScript merge two arrays and remove repeated items
- Examples of merging multiple arrays using native JavaScript
- Comparison of several methods and advantages of JS array Merging
- Use different methods to combine/merge two JS Arrays
- N methods for merging arrays in JavaScript
- JavaScript and jquey Merge multiple arrays
- How to merge arrays in JavaScript