我對JS集合的簡單學習

我對集合的學習

第一步知道相關概念

集合是由一組無序且惟一的項組成的,在ES6中已經實現了相似的Set類。javascript

function Set() {
    var items = {}; //items就是集合
}

第二步實現相關操做方法

首先實現has(value)方法,判斷值是否在集合中,返回布爾值。由於集合是不容許重複元素存在的,其餘方法調用這個方法判斷是否值已存在。java

this.has = function(value) {
    return value in items; //in操做符
};

還有另一種實現方法,hasOwnProperty()方法返回代表一個對象是否具備特定屬性的布爾值。數組

this.has = function(value) {
    return items.hasOwnProperty(value);
};

接下來實現add()方法,向集合中添加一個新的項。學習

this.add = function(value) {
    if(!this.has(value)) { //判斷要添加的元素是否已經存在
        items[value] = value; //注意同時做爲鍵和值保存,有利於查找這個值
        return true;
    }
    return false; //添加的元素已經存在,再也不添加
};

接下來實現remove()方法,從集合中移除一個值。this

this.remove = function(value) {
    if(this.has(value)) {
        delete items[value]; //刪除對象的屬性
        return true;
    }
    return false;
};

接下來實現clear()方法,移除集合全部值。code

this.clear = function() {
    items = {}; //空對象從新賦值給它
};

接下來實現size()方法,返回集合中有多少項。對象

  • 第一種實現方法,使用一個length變量ip

  • 第二種實現方法,使用Object.keys()rem

this.size = function() {
    return Object.keys(items).length; //返回一個對象的全部可枚舉的屬性名組成的數組,但不包括原型中的屬性,也不能保證按照順序輸出屬性名
};
  • 第三種實現方法,手動提取items對象的每個屬性,記錄個數原型

this.size = function() {
    var count = 0;
    for(var prop in items) { //遍歷items的全部屬性
        if(items.hasOwnProperty(prop)) { //防止計數時計算到原型的屬性
            ++count;
        }
    }
    return count;
};

實現values()方法,返回全部值組成的數組。

this.values = function() {
    return Object.keys(items); //得到鍵也就得到了值(由於他們同樣啊)
};

第三步簡單使用Set類

var set = new Set();
set.add(1); 
console.log(set.values()); //['1']
console.log(set.has(1)); //true
console.log(set.size()); //1

set.add(2);
console.log(set.values()); //['1', '2']

set.remove(1); 
console.log(set.values()); //['2']

第四步操做集合

  • 並集:兩個集合,返回一個包含兩個集合中全部元素的新集合

能夠用來合併兩個元素,並且保證了單一性。

this.union = function(otherSet) {
    var unionSet = new Set(); //並集結果
    var values = this.values();
    for(var i = 0; i < values.length; i++) { //遍歷第一個集合所有放到新集合
        unionSet.add(values[i]);
    }
    values = otherSet.values();
    for(var i = 0; i < values.length; i++) { //遍歷第二個集合所有放到新集合,使用了add方法保證了單一性
        unionSet.add(values[i]);
    }
    return unionSet;
}
  • 交集:兩個集合,返回一個包含兩個集合中共有元素的新集合

能夠用來取二者共有的部分。

this.intersection = function(otherSet) {
    var intersectionSet = new Set();
    var values = this.values();
    for(var i = 0; i < values.length; i++) {
        if(otherSet.has(values[i])) {
            intersectionSet.add(values[i])    
        }
    }
    return intersectionSet;
}
  • 差集:元素存在於A且不存在於B中

this.difference = functiong(otherSet) {
    var differenceSet = new Set();
    var values = this.values();
    for(var i = 0; i < values.length; i++) { //遍歷了A
        if(!otherSet.has(values[i])) { 
            differenceSet.add(values[i]);
        }
    }
    return differenceSet;
}
  • 子集:A中的每個元素都在B中

this.subset = function(otherSet) {
    if(this.size() > otherSet.size()) return false;
    var values = this.values();
    for(var i = 0; i < values.length; i++) {
        if(!otherSet.has(values[i])) { //只要有一個A中的元素不在B中,返回false
            return false;    
        }
    }
    return true;
}
相關文章
相關標籤/搜索