1. What is SetSet can be simply thought of as a mathematical set. It is an unordered collection of data with no repeated values . 2. Set ConstructorFor the parameters of the Set constructor, the following forms can be passed. 2.1) Arraysconst s = new Set([1, 2, 1]); console.log(s); Here, an array 2.2) Stringsconst s = new Set("Hello World!"); console.log(s); 2.3) argumentsfunction fun() { const s = new Set(arguments); console.log(s); } fun(1, 2, 3); 2.4) NodeList<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>set</title> </head> <body> <p>1</p> <p>2</p> <p>3</p> <script> const s = new Set(document.querySelectorAll('p')); console.log(s); </script> </body> </html> Here, references to three When we need to use it, we can traverse this 2.5) Setconst s1 = new Set([1, 2, 3]); const s2 = new Set(s1); console.log(s2); This is equivalent to copying console.log(s1 === s2); 3. Set instance properties and methods The properties of Set have an attribute const s = new Set([1, 2, 3]); console.log(s.size); Methods of Set Adding members to a Set const s = new Set([1, 2, 3]); // It can only pass one parameter s.add(5); console.log(s); // It can be concatenated adds.add(7).add(9); console.log(s); Used to delete members from a Set const s = new Set([1, 2, 3]); s.delete(2); // If the item to be deleted is not found in the Set, nothing will happen and no error will be reported s.delete(5); console.log(s); Used to determine whether a Set contains a member const s = new Set([1, 2, 3]); console.log(s.has(1)); console.log(s.has(5)); Will delete all members of the Set const s = new Set([1, 2, 3]); s.clear(); console.log(s); 4. Set member access Its member access is implemented through the It has two parameters, the first parameter is the callback function, and the second parameter sets what s.forEach(callback function, pointer to callback function) Let's look at the first parameter: For the first parameter callback function, it has three parameters: s.forEach(function(value, key, set){ value is a member of Set. In Set, value and key are equal. Set is the previous Set itself, that is, here set === s }); Let's understand it through an example: const s = new Set([1, 2, 3]); s.forEach(function(value, key, set) { console.log(value, key, value === key); console.log(set, set === s); }); Let's look at the second parameter: const s = new Set([1, 2, 3]); s.forEach(function(value, key, set) { console.log(this); }, document); 5. Notes on Set Set's judgment on duplicate values basically follows the strict equality However, for 6. Use cases of SetArray deduplication let arr = [1, 2, 1]; const s = new Set(arr); arr = [...s]; // You can also combine them into one sentence // arr = [...new Set(arr)]; console.log(arr); String deduplication let str = "11231131242"; const s = new Set(str); str = [...s].join(""); // Can also be written as one sentence // str = [...new Set(str)].join(""); console.log(str); Storing DOM elements <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>set</title> </head> <body> <p>1</p> <p>2</p> <p>3</p> <script> const s = new Set(document.querySelectorAll('p')); s.forEach((elem) => { console.log(elem) }); </script> </body> </html> SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: The best 9 foreign free picture material websites
>>: A brief analysis of the usage of HTML float
Table of contents 1. Basic SELECT statement 1. Qu...
Shell Script #!/bin/sh # Current directory CURREN...
Table of contents 1. Development Environment 2. I...
Table of contents What is a container data volume...
IE gave us a headache in the early stages of deve...
Table label composition The table in HTML is comp...
Table of contents Why is IN slow? Which is faster...
After the release of CentOS8.0-1905, we tried to ...
Overview In a relational database, an index is a ...
The first step is to install TypeScript globally ...
Table of contents vite Build Configuration vite.c...
Phenomenon: After MySQL version 5.7, the default ...
A status code that indicates a provisional respon...
I was working on a pop-up ad recently. Since the d...
http://www.cppcns.com/shujuku/mysql/283231.html Y...