What are the es6 array merging methods?

青灯夜游
Release: 2022-05-05 18:17:09
Original
12906 people have browsed it

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);
Copy after login

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)
Copy after login

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));
Copy after login

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]);
Copy after login

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!

Related labels:
es6
source:php.cn
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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!