Home >Web Front-end >Front-end Q&A >What are the es6 array merging methods?

What are the es6 array merging methods?

青灯夜游
青灯夜游Original
2022-05-05 18:16:4013028browse

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.

What are the es6 array merging methods?

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);

What are the es6 array merging methods?

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));

What are the es6 array merging methods?

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]);

What are the es6 array merging methods?

## [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!

Statement:
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