Home > Web Front-end > JS Tutorial > How to use the ES6 spread operator

How to use the ES6 spread operator

php中世界最好的语言
Release: 2017-12-30 17:37:15
Original
2507 people have browsed it

This time I will show you how to use the ES6 expansion operator. The expansion operator has the following four functions. This article will give you a good analysis.

1. Expand the array

//展开数组
let a = [1,2,3,4,5],
b = [...a,6,7];
console.log(b);
//打印出来的值[1, 2, 3, 4, 5, 6, 7]
Copy after login

2. Copy the array

//数组的拷贝
var c = [1, 2, 3];
var d = [...c]; 
d.push(4); 
console.log(d);
//打印出来的值[1, 2, 3, 4]
Copy after login

3. Merge the array

//数组的合并
var j = [7, 1, 2];
var k = [5, 0, 8];
j = [...k, ...j];
console.log(j)
//打印出来的值[5, 0, 8, 7, 1, 2]
Copy after login

4. Calling the spread function

//展开函数调用
function fn(a,b,c,d){
 console.log(a+b+c+d);
}
var p=[1,9,3,,6];
let result=fn(5,...p);开函数的调用
//打印出来的值18
Copy after login

The spread operator (spread) is three dots (...), which converts an array ||like array| |Convert a string to a comma-separated sequence. This guy is used to operate arrays and take out all the things in the array

I believe you have mastered the method after reading the above introduction. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related reading:

js implements imitation window system calendar effect

How can JS click to jump to login Personal email

There are several ways to implement chain operations in PHP

The above is the detailed content of How to use the ES6 spread operator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template