函数集合
/* * @Author: yongyuan at <yongyuan253015@gmail.com> * @Date: 2021-08-01 22:59:12 * @LastEditTime: 2021-08-01 23:39:16 * @LastEditors: yongyuan at <yongyuan253015@gmail.com> * @Description: 函数实现集合 * @FilePath: \JavaScript\collection\collection-function.js * */function Set() { let items = {}; this.has = function (value) { return items.hasOwnProperty(value) } this.add = function (value) { if (!this.has(value)) { items[value] = value; return true; } return false; } this.remove = function (value) { if (this.has(value)) { delete items[value]; return true; } return false } this.clear = function () { items = {} } this.size = function () { return Object.keys(items).length; } this.values = function () { return Object.values(items) } // 并集 this.union = function (otherSet) { const unionSet = new Set(); this.values().map((item) => { unionSet.add(item) }) otherSet.values().map((item) => { unionSet.add(item) }) return unionSet } // 交集 this.intersection = function (otherSet) { const intersectionSet = new Set(); const values = this.values(); const otherValues = otherSet.values(); let biggerSet = values; let smallerSet = otherValues; if (otherValues.length - values.length > 0) { biggerSet = otherValues; smallerSet = values; } smallerSet.map((item) => { if (biggerSet.includes(item)) { intersectionSet.add(item) } }) return intersectionSet; } // 差集 this.difference = function (otherSet) { const differenceSet = new Set(); this.values().map((item) => { if (!otherSet.has(item)) { differenceSet.add(item); } }); return differenceSet; } // 子集 this.isSubsetOf = function (otherSet) { if (this.size() > otherSet.size()) { return false; } let isSubset = this.values().every((value) => otherSet.has(value)); return isSubset }}const set = new Set();set.add(1);set.add(2);set.add(31);set.add(0);console.log(set.values());console.log(set.has(3));console.log(set.size());const setB = new Set();setB.add(10);setB.add(0);console.log(set.union(setB).values());console.log(set.intersection(setB).values());console.log(set.difference(setB).values());console.log(set.isSubsetOf(setB));
Class集合
/* * @Author: yongyuan at <yongyuan253015@gmail.com> * @Date: 2021-07-30 23:21:43 * @LastEditTime: 2021-07-31 18:06:52 * @LastEditors: yongyuan at <yongyuan253015@gmail.com> * @Description: 集合的类声明 * @FilePath: \JavaScript\collection\collection-class.js * */class Set { constructor() { this.items = {}; // 用对象来表示集合 } has(element) { // Object原型有hasOwnProperty方法,它返回一个表明对象是否具有特定属性的布尔值 // in 运算符返回的是对象在原型链上是否具有特定属性的布尔值 return Object.prototype.hasOwnProperty.call(this.items, element) } // 往集合添加成员 add(element) { if (!this.has(element)) { this.items[element] = element; return true; } return false; } // 删除集合成员 delete(element) { if (this.has(element)) { delete this.items[element]; return true; } return false; } // 清空集合成员 clear() { return this.items = {} } // 返回集合大小 size() { return Object.keys(this.items).length; } // 返回的集合成员的值 values() { return Object.values(this.items) } // 集合运算:并集、交集、差集和子集 union(otherSet) { const unionSet = new Set(); this.values().forEach(value => { unionSet.add(value); }); otherSet.values().forEach((value) => { unionSet.add(value); }) return unionSet } // 交集 intersection(otherSet) { const intersectionSet = new Set(); const values = this.values(); const otherValues = otherSet.values(); let biggerSet = values; let smallerSet = otherValues; if (otherValues.length - values.length > 0) { biggerSet = otherValues; smallerSet = values; } smallerSet.forEach((value) => { if (biggerSet.includes(value)) { intersectionSet.add(value) } }) return intersectionSet; } // 差集 difference(otherSet) { const differenceSet = new Set(); this.values().forEach(value => { if (!otherSet.has(value)) { differenceSet.add(value); } }); return differenceSet; } // 子集 isSubsetOf(otherSet) { if (this.size() > otherSet.size()) { return false } let isSubset = this.values().every(value => otherSet.has(value)); return isSubset }}const set = new Set();set.add(1)console.log(set.values()); // [1]console.log(set.has(2));// falseconst setA = new Set();const setB = new Set();setA.add(1)setA.add(2)setB.add(1)setB.add(3)setB.add(2)const unionAB = setA.union(setB)console.log(setA)console.log(setB)console.log("并集", unionAB)const intersectionAB = setA.intersection(setB)console.log("交集", intersectionAB)const differenceAB = setB.difference(setA);console.log("差集", differenceAB)console.log("集合A是集合B的子集", setA.isSubsetOf(setB))console.log(new Set([{a:1,a:1,a:2,b:3}]))
ES6集合
/* * @Author: yongyuan at <yongyuan253015@gmail.com> * @Date: 2021-08-01 23:59:34 * @LastEditTime: 2021-08-02 23:35:42 * @LastEditors: yongyuan at <yongyuan253015@gmail.com> * @Description: ES6的set结构实现集合 * @FilePath: \JavaScript\collection\collection-ES6.js * * 数据结构set,类似数组,其成员值是唯一的,没有重复值。Set本身是一个构造函数。 * * add() * * size * * has() * * delete() * * clear() * * 可以给数组去重,也可以给对象去重,去重相同属性。 */const _set = new Set();_set.add({ name1: "duxin1", name2: "duxin2", name: "duxin3" })console.log(_set)console.log(_set.size)const _setB = new Set([1, 2, 3, 4, 3]);console.log("A集合", _set)console.log("B集合", _setB);//并集let union = new Set([..._set, ..._setB]);console.log("并集", union)// 交集let intersect = new Set([..._set].filter(value => _setB.has(value)))console.log("交集", intersect)// 差集const difference = new Set([..._set].filter(value => !_setB.has(value)));console.log("差集", difference)