Home > Web Front-end > JS Tutorial > Analysis of the difference between JS array merge push and concat_javascript skills

Analysis of the difference between JS array merge push and concat_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 15:25:18
Original
2515 people have browsed it

The example in this article describes the difference between JS array merge push and concat. Share it with everyone for your reference, the details are as follows:

Pay attention to the spelling of concat. The two functions are very similar, but there are two differences.

Look at the code first:

var arr = [];
arr.push(1);
arr.push([2, 3]);
arr.push(4, 5);
arr = arr.concat(6);
arr = arr.concat([7, 8]);
arr = arr.concat(9, 10);
arr.each(function(index, value){
  alert(value);
});
Copy after login

alert result:

1
2,3
4
5
6
7
8
9
10

Copy after login

Difference:

When push encounters an array parameter, it treats the entire array parameter as one element; while concat splits the array parameter and adds it element by element.
push directly changes the current array; concat does not change the current array.

Summary:

If you want to append to the array, use concat, but it is the same as java's replace. Remember arr1=arr1.concat(arr2)

I hope this article will be helpful to everyone in JavaScript programming.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template