JS/TS写一个Dictionary类_艾孜尔江撰稿

export default class Dictionary {

    private dataStore: any = new Array<any>(); // must be instatiated! 

    Dictionary() {
        this.dataStore = [];
    }

    public add = (key: any, value: any) => {
        this.dataStore[key] = value;
    }

    public find = (key: any) => {
        return this.dataStore[key];
    }

    public getKeyByValue = (value: any) => {
        for (const key in this.dataStore) {
            if (this.dataStore[key] === value) {
                return key;
            }
        }
        return 'Not Found';
    }

    public remove = (key: any) => {
        if (this.dataStore[key]) delete this.dataStore[key];
        else return 'Not Found';
    }

    public showAll = () => {
        for (const key in this.dataStore)
            console.log(key + '->' + this.dataStore[key]);
    }
}

上一篇:【Python】判断一个数组中是否包含另一个数组


下一篇:TypeScript 高级用法