First, Set1. Definition
The set object is a newly defined data structure in ES6, similar to an array, which allows you to store a unique value of any type, whether it is a raw value or an object reference.
2. Syntax
new Set([iterable])
- Iterable: Object can be iterated, default is empty.
Set method
- Add: Adds a value that returns the set itself.
- Delete: Deletes the value and returns whether the deletion succeeded.
- Has: Determines if it has this value and returns TRUE/FALSE.
- Clear: Clears all values.
3. Example
let s = new Set();s.add(4);s.add(1);s.add(3);s.add(3);s.add(2);s.add(2);console.log(s); // {4, 1, 3, 2}console.log(s.has(4)); // trues.delete(4);console.log(s); // {1, 3, 2}console.log(s.has(4)); // falses.clear();console.log(s); // {}
Second, through the set array to weight
The extension operator allows you to convert a set into a true array.
let arr = [4, 1, 3, 3, 2, '2'];let uniqueArr = [...new Set(arr)];console.log(uniqueArr); // [4, 1, 3, 2, "2"]
ES6 through set array to remove weight