Implementation method:
let arr1 = [1, 2, 3, 4, 5, 6];
let arr2 = [2, 4, 6, 7, 8];
let arr3 = Array.from(new Set([...arr1, ...arr2])); Copy the code
Extending ES6 Set
A Set is like an array, but its member values are unique and unique.
let set= new Set (,2,2,3,3,4 [1]); Set(4) {1, 2, 3, 4}Copy the code
Set operation method
Add (value): Adds a value and returns the Set function.
Delete (value): deletes a value.
Has (value): Returns a Boolean value indicating whether it is a member of a Set
Clear (): Clears all members
set.add('1').add('a');
set.has(1); //true
set.has('1'); //true
set.has('a'); //true
set.delete('1');
set.clear(); Copy the code
Array.from converts a Set structure to an Array, which allows Array deduplicating
ler arr = Array.from(set); Copy the code
Set traversal method
Keys (): Returns all key names
Values (): Returns all key values
Entries (): Returns all key-value pairs
ForEach (): callback function traversal
The keys, values, and entries methods all return traverser objects. Because the Set structure has no key name, only key value (that is, the key name and key value are the same value), the key and value methods behave exactly the same.
set.values(); //1, 2, 3, 4
set.keys(); //1, 2, 3, 4
set.entries(); //0: {1 => 1}1: {2 => 2}2: {3 => 3}3: {4 => 4}
set.forEach((item)=>console.log(item));Copy the code