Set the value of the object is always stored only
Set object methods
method | description |
---|---|
add | Adding a value to return to Set the object itself. |
clear | Delete all of the key / value pairs, there is no return value. |
delete | Delete a key, returns true. If the deletion fails, returns false. |
forEach | Perform the specified operation on each element. |
has | It returns a Boolean value that indicates whether a key target in the current Set. |
Set Object Role
- Deduplication Array
var arr = [1,2,3,3,1,4];
[...new Set(arr)]; // [1, 2, 3, 4]
Array.from(new Set(arr)); // [1, 2, 3, 4]
[...new Set('ababbc')].join(''); // "abc" 字符串去重
new Set('ice doughnut'); //["ice","doughnut"]
- Union
var a = new Set([1, 2, 3]);
var b = new Set([4, 3, 2]);
var union = new Set([...a, ...b]); // {1, 2, 3, 4}
- Intersection
var a = new Set([1, 2, 3]);
var b = new Set([4, 3, 2]);
var intersect = new Set([...a].filter(x => b.has(x))); // {2, 3}
- Difference set
var a = new Set([1, 2, 3]);
var b = new Set([4, 3, 2]);
var difference = new Set([...a].filter(x => !b.has(x))); // {1}