集合是由一組無序且惟一(不能重複)的項組成的,與咱們高中數學學的集合一樣具備有限性,
這種數據結構大多數用來存儲惟一元素。數據庫
class Set { constructor() { this.items = {}; }
has(element) { 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() { this.items = {}; };
size() { return Object.keys(this.items).length; }; //另一種方式 /* sizeLegacy(){ let count = 0; for(let key in this.items){ if(this.items.hasOwnProperty(key)){//判斷是不是對象的屬性,避免重複計數 count++; } return count; }; } */
valuesLegacy(){ let values = []; for(let key in this.items){ if(this.items.hasOwnProperty(key)){ values.push(key); }; }; return values; } //另外一種寫法,只適用於現代瀏覽器 values() { return Object.values(this.items); }
以上就是集合數據結構的建立和成員函數的添加。而集合最主要的是它的運算,在計算機領域中應用最多的是數據庫,發送一條SQL查詢命令時,使用的是集合運算,返回的也是數據集合。接下來介紹相對常見的集合運算(如下代碼引用ES6語法)數組
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 = true; this.values().every(value => { if (!otherSet.has(value)) { isSubset = false; return false; } return true; }); return isSubset; }
以上是咱們實現的集合運算,可是ES2015原聲的Set並無這些功能,若是有須要的話,咱們也能夠模擬。瀏覽器
const union = (set1, set2) => { const unionAb = new Set(); set1.forEach(value => unionAb.add(value)); set2.forEach(value => unionAb.add(value)); return unionAb; }; //第二種:使用拓展運算符 new Set([...setA, ...setB]);
const intersection = (set1, set2) => { const intersectionSet = new Set(); set1.forEach(value => { if (set2.has(value)) { intersectionSet.add(value); } }); return intersectionSet; }; //第二種:使用拓展運算符 new Set([...setA].filter(x => setB.has(x)))
const difference = (set1, set2) => { const differenceSet = new Set(); set1.forEach(value => { if (!set2.has(value)) { differenceSet.add(value); } }); return differenceSet; }; //第二種:使用拓展運算符 new Set([...setA].filter(x => !setB.has(x)))