哈希其实就是js⾥的对象,它在实际的键值和存⼊的哈希值之间存在⼀层映射。如下例⼦:
class HashTable{
constructor() {
this.iems={}
}
put(key,value){
const hash=this.keyToHash(key)
this.items[hash]=value
}
get(key){
return this.items[this.keyToHash(key)]
}
remove(key){
delete (this.items[this.keyToBash(key)])
}
keyToHash(key){
let hash=0
for(let i=0;i<key.length;i++){
hash+=key.charCodeAt(i)
}
hash=hash%37
return hash
}
}
let kkb=new HashTable()
kkb.put("name","nihao")
kkb.put("age","6")
kkb.put("best","大胜老师")
console.log(kkb.get("name"))
console.log(kkb.get("best"))
kkb;.remove("name")
console.log(kkb.get("name"))