This article describes the application of ES6 Set structure. Share it for your reference, as follows:
Set is similar to an array, but the values of members are unique and there are no duplicate values, which implements the iterator interface
The value of set cannot be repeated, the value of the array can be repeated
let arr = [1,2,3,'5','5']; let st = new Set(arr); (st); // You can remove duplicate values of the array through set, and the returned pseudo-array(); // 4
set's add , delete, has, clear method
Simple add and delete:
let st = new Set(); var u = {name:'Joh'}; (u); let bool = (u); (bool); // true;
Continuous add with has api:
let st = new Set(); var u = {name:'Joh'}; var r = {name:'Lily'}; (u).add(r); let bool = (r); (bool); // true ((r)); // false ((u)); // true;
clear clear set collection
let st = new Set(); var u = {name:'Joh'}; var r = {name:'Lily'}; (u).add(r); (); (); // 0
Convert an array-like model into an array by method
let arr = ['xxx', 'yyyy', 'yyyy']; let newArr = (new Set(arr)); ((newArr)); // true (newArr); // ["xxx", "yyyy"]
The Set prototype and values are the same value, and can be traversed directly for-of
([] === ); // true let st = new Set(['xxx', 'yyyy', 'yyyy', 'John']); for(let k of st) { (k); // Output xxx yyy John can directly traverse, compatible with map's data structure}
Keys and values methods in set
let st = new Set(['xxx', 'yyyy', 'yyyy', 'John']); (); // 3 let itKeys = (); for(let k of itKeys) { (k); // Output xxx yyy John in turn} ('>>>>>'); let itVals = (); for(let v of itVals) { (v); // Output xxx yyy John in turn}
set entries entity object is an array structure of keys and values
let st = new Set(['xxx', 'yyyy', 'yyyy', 'John']); let entriesIt = (); // for(let v of entriesIt) { (v); // Output ["xxx", "xxx"] ["yyyy", "yyyy"] ["John", "John"]}
About the special nature of NaN in set
let st = new Set(); (NaN === NaN); // false , NaN is incomplete here, so you should be able to add multiple items, not count as duplication, but here is a special example(NaN).add(NaN).add(NaN); for(let v of st) { (v); // Only output one NaN}
For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript array operation skills》、《Summary of JavaScript sorting algorithm》、《Summary of JavaScript mathematical operations usage》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary》、《Summary of JavaScript search algorithm skills"and"Summary of JavaScript Errors and Debugging Skills》
I hope this article will be helpful to everyone's JavaScript programming.