Set is the basic mathematical knowledge, which is also very common in program application. But there are not many sets checked on the Internet through js, some of them are realized by using more traditional methods. Of course, it is not that the traditional methods have any shortcomings, and the compatibility of the traditional methods is better
I found that in ES6, Set class is naturally suitable for collection. Because Set has element uniqueness, this feature is also one of the most basic features of collection, so it can realize class abstraction with simple code
The common operations of a class include find, add, delete, gather, union, difference Set, subset, etc. it is very convenient to implement these functions through Set. The following is the code to implement a simple Set class
/** * Collection class */ export default class aggregate { //Constructor constructor(array) { let _init = array instanceof Array ? array : null; this._set = new Set(_init); } //New collection add(newElement) { this._set.add(newElement); return this.getValues(); } /** * Whether the collection contains * @param {[type]} element [description] * @return [Include this element or not] */ has(element) { return this._set.has(element); } //Set delete specified remove(element) { this._set.delete(element); return this.getValues(); } //Empty collection clear() { this._set.clear(); } /** * Set size, can judge whether it is an empty set * @return [Set size] */ size() { return this._set.size; } /** * Collection content * @return [Collection content array] */ getValues() { return [...this._set]; } /** * Union, all elements of two sets * @param {[type]} otherSet [Target set] * @return [Union] */ union(otherSet) { return new Set([...this._set,...otherSet.getValues()]); } /** * Intersection, elements of common ownership * @param {[type]} otherSet [Target set] * @return [Intersection] */ intersection(otherSet) { return new Set(this.getValues().filter((val) => otherSet.has(val))); } /** * Difference set * @param {[type]} otherSet [Target set] * @return [Difference set] */ difference(otherSet) { return new Set(this.getValues().filter(val => !otherSet.has(val))); } /** * subset * @param {[type]} otherSet [Target set] * @return [Whether the target set is a subset] */ isSubset(otherSet) { let _isSubset = otherSet.getValues().every(val => this._set.has(val)); return _isSubset; } /** * Equal * @param {[type]} otherSet [Target set] * @return [Is it equal to the target set] */ isEqual(otherSet) { let _subset1 = this.isSubset(otherSet); let _subset2 = this.getValues().every(val => otherSet.has(val)); return _subset1 && _subset2; } }
The above is just a simple implementation of the abstraction of Set classes. The above is only to consider the operation between two sets, but also to consider the operation compatible with multiple sets more deeply, such as the aggregation larger than two sets, etc. there are some collection methods that are not implemented, such as complement, etc. in this case, the Set class is simply used. The Set class is also very suitable for the practice and learning of Set