Skip to content
Spotify - 每月低于 10 元

第3章 集合与字典

集合

js
class Set {
    constructor() {
        this.items = {};
    }

    //1.判断某个元素是否在集合中
    has(element) {
        return element in this.items;
    }

    //2.向集合添加一个新元素
    add(element) {
        if (!this.has(element)) {
            this.items[element] = element;
            return true;
        }
        return false;
    }

    //3.从集合中移除一个元素
    delete(element) {
        if (this.has(element)){
            delete this.items[element];
            return true;
        }
        return false;
    }

    //4.移除集合中的所有元素
    clear() {
        this.items = {};
    }

    //5.返回集合所含元素的数量
    size() {
        return Object.keys(this.items).length;
    }
    
    //6.返回一个包含集合中所有值的数组
    values() {
        return Object.keys(this.items);
    }
}

集合间操作

js
//1.并集
union(otherSet) {
    //创建新的集合作为两个集合的并集
    const unionSet = new Set();
    //遍历两个集合并添加到新的集合中
    this.values().forEach(value => unionSet.add(value));
    otherSet.values().forEach(value => unionSet.add(value));
    return unionSet;
}

//2.交集
intersection(otherSet) {
    const intersectionSet = new Set();
    const values = this.values();
    for (let i = 0; i < values.length; i++) {
        if (otherSet.has(values[i])) {
            intersectionSet.add(values[i]);
        }
    }
    return intersectionSet;
}

//3.差集
difference(otherSet) {
    const differenceSet = new Set();
    this.values().forEach(value => {
        if (!otherSet.has(value)) {
            differenceSet.add(value);
        }
    })
    return differenceSet;
}

//4.子集
isSubsetOf(otherSet) {
    if (this.size() > otherSet.size()) {
        return false;
    }
    let isSubset = true;
    this.values().every(value => {
        //若原来Set中的元素不在otherSet中,回调函数返回false,循环停止
        if (!otherSet.has(value)) {
            isSubset = false;
            return false;
        }
        return true;
    });
    return isSubset;

字典

js
//Map中理想的情况使用字符串作为键名,该函数用于将key转换为String
function defaultToString(item) {
    if (item === null) {
        return 'NULL';
    } else if (item === undefined) {
        return 'UNDEFINED'
    } else if (typeof item === 'string' || item instanceof String) {
        return `${item}`;
    }
    return item.toString();
}


//ValuePair类用于存储原始的key和value
class ValuePair {
    constructor(key, value) {
        this.key = key;
        this.value = value;
    }

    toString() {
        return `[#${this.key}:${this.value}]`;
    }
}

class Dictonary {
    constructor(toStrFn = defaultToString) {
        this.toStrFn = toStrFn;
        this.table = {};    //用于保存键值对
    }

    //1.检测一个键是否在字典中
    hasKey(key) {
        return this.table[this.toStrFn(key)] !== null;
    }

    //2.设置键值对
    set(key, value) {
        if (key != null && value != null) {
            const tableKey = this.toStrFn(key);
            this.table[tableKey] = new ValuePair(key, value);
            return true;
        }
        return false;
    }

    //3.从字典中移除一个值
    remove(key) {
        if (this.hasKey(key)) {
            delete this.table[this.toStrFn(key)];
            return true;
        }
        return false;
    }

    //4.从字典中检索一个值
    get(key) {
        const valuePair = this.table[this.toStrFn(key)];
        return valuePair == null ? undefined : valuePair.value;
    }

    //5.以数组形式返回字典中所有valuePair对象
    keyValues() {
        return Object.values(this.table);
    }

    //6.以数组形式返回斯所有的原始Key
    keys() {
        //map方法可以将valuePair转换为valuePair.key
        return this.keyValues().map(valuePair => valuePair.key);
    }

    //7.以数组形式返回斯所有的Value
    values() {
        return this.keyValues().map(valuePair => valuePair.value);
    }

    //8.用forEach迭代字典中的每个键值对
    forEach(callbackFn) {
        const valuePairs = this.keyValues();
        for (let i = 0; i < valuePairs.length; i++) {
            const result = callbackFn(valuePairs[i].key, valuePairs[i].value);
            if (result === false) {
                break;
            }

        }
    }

    //9.返回字典中值的个数
    size() {
        return Object.keys(this.table).length;
    }

    //10.判空
    isEmpty() {
        return this.size() === 0;
    }

    //11.清空字典
    clear() {
        this.table = {};
    }

    //12.转换为字符串
    toString() {
        if (this.isEmpty()) {
            return '';
        }
        const valuePairs = this.keyValues();
        let objString = `${valuePairs[0].toString()}`;
        for (let i = 1;i < valuePairs.length; i++) {
            objString = `${objString},${valuePairs[i].toString()}`;
        }
        return objString;
    }
}

const dictionary = new Dictonary();
//使用forEach
dictionary.forEach((k,v) => {
    console.log("forEach",`key:${k},value:${v}`);
})
关注微信公众号V.PS- 美国 CN2 GIA / 9929 / CMIN2 顶级线路
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0

预览:

评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3