字典
2020-05-30 本文已影响0人
skoll
简介
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