字典

作者: skoll | 来源:发表于2020-05-30 20:21 被阅读0次

    简介

    1 .以键值对存储数据的数据结构

    class Dict{
        constructor(){
            this.dataStore=[]
            // 这里并不是用的object,而是用的array来存储
            this.dataLength=0
        }
        add(key,value){
            this.dataStore[key]=value
            this.dataLength++
            console.log(this.dataStore)
        }
        find(key){
            return this.dataStore[key]
        }
        remove(key){
            delete this.dataStore[key]
            this.dataLength--
        }
        showAll(){
            for(let key in this.dataStore){
                console.log(key,this.dataStore[key])
            }
        }
        count(){
            return this.dataLength
        }
        clear(){
            this.dataStore=[]
            this.dataLength=0
        }
    }
    
    var pbook = new Dict(); 
    pbook.add("Mike","123");
    pbook.add("David", "345");
    pbook.add("Cynthia", "456");
    pbook.showAll();
    pbook.remove("Cynthia");
    console.log(pbook.count());//2
    pbook.clear();
    console.log(pbook.count());//0
    

    相关文章

      网友评论

          本文标题:字典

          本文链接:https://www.haomeiwen.com/subject/ccwhzhtx.html