Home > Article > Web Front-end > What are the es6 array merging methods?
3 methods: 1. Use "for(i in array2){array1.push(array2[i])}" to traverse the array and add the value of one array to the end of another array ; 2. Use the "array1.concat(array2...)" statement to connect multiple arrays; 3. Use the "[...array1,...array2]" statement.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
es6 array merging method
Method 1: Using for in loop
var a = [1,2,3]; var b = [4,5,6]; console.log(a); console.log(b); for(var i in b){ a.push(b[i]); } console.log(a);
Method 2: Use concat()
The concat() method is used to connect two or more arrays.
array1.concat(array2,array3,...,arrayX)
will return a new array. The array is generated by adding all arrayX parameters to arrayObject. If the argument to concat() is an array, the elements in the array are added, not the array.
var a = [1,2,3]; var b = [4,5,6]; console.log(a); console.log(b); console.log(a.concat(b)); console.log(b.concat(a));
Method 3: Use the spread operator "..."
var a = [1,2,3]; var b = [4,5,6]; console.log(a); console.log(b); console.log([...a,...b]);## [Related recommendations:
javascript video tutorial, web front-end]
The above is the detailed content of What are the es6 array merging methods?. For more information, please follow other related articles on the PHP Chinese website!