Home > Web Front-end > JS Tutorial > body text

How to use Spread operator (...) in JavaScript? 8 methods introduced

青灯夜游
Release: 2020-06-29 10:40:38
forward
2557 people have browsed it

How to use Spread operator (...) in JavaScript? 8 methods introduced

The spread operator (...) was introduced in ES6.

The spread operator expands an iterable object, which is any object that can be looped using a for loop, into its individual elements.

Iterable examples: Array, String, Map, Set, DOM nodes.

1. Use the spread operation operator in log

You can iterate in console.log The object uses the spread operation operator

How to use Spread operator (...) in JavaScript? 8 methods introduced

2. Use the spread operation operator to copy the array

How to use Spread operator (...) in JavaScript? 8 methods introduced

Copy object

let user = {name : "John", age : 20 }
let userCopy = {...user}
Copy after login
The stretch operation operator does not perform a deep copy.

3. Extended operation operator merge

How to use Spread operator (...) in JavaScript? 8 methods introduced

Merge objects

Merge objects , if a key already exists, it is replaced with the last object with the same key.

let user1 = {name : "John", age : 20 };
let user2 = {name : "Ram", salary: '20K' };
let userCopy = {...user1, ...user2};
userCopy ; // {name : "Ram", age :20 , salary : '20K'};
Copy after login

4. The spread operation operator is passed as a parameter

function sum(a, b) {
   return a+b;
}
let num = [1,2];
sum(...num); // 3
Copy after login

and math Functions are used together

let num = [5,9,3,5,7];
Math.min(...num);
Math.max(...num);
Copy after login

5. The extension operator is used in destructuring variables

How to use Spread operator (...) in JavaScript? 8 methods introduced

Destructuring the object

let user = {name : "Ram", age: 20, salary: '20K', job : "Tester" };
let { name, age, ...details } = user;
name; // Ram
age; // 20
details; // {salary: '20K', job : 'Tester'};
Copy after login

6. Convert the NodeList object to an array

NodeList Similar to an array, but without all the methods of Array, such as forEach, map, filter, etc.

let nodeList = document.querySelectorAll('.class')
var nodeArray = [...nodeList]
Copy after login

7. Convert strings to characters

Strings are also iterable objects, so we can also use . .. to string.

let name = "Ram";
let chars = [...name]; // ["R", "a", "m"]
Copy after login

8. Remove duplicates from an array

let num = [1, 3, 1, 3, 3, 1];
let uniqueNum = [...new Set(num)];
uniqueNum; //[ 1, 3 ]
Copy after login

Original address: https://medium.com/javascript- in-plain-english/8-ways-to-use-spread-operator-in-javascript-b66fcf016efe

Author: Javascript Jeep

Reprinted from: https://segmentfault.com/ a/1190000023023909

Recommended related tutorials: JavaScript video tutorial

The above is the detailed content of How to use Spread operator (...) in JavaScript? 8 methods introduced. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!