集合、字典、散列表都可以存储不重复的数据。字典和我们上面实现的集合很像,集合中我们以{ value: value }的形式存储数据,而字典是以{ key: value }的形式存储数据,字典也称作映射。
实现一个字典数据
// 实现一个字典数据
class Dictonary {
constructor(){
this.item = {}
}
set(key, value){
this.item[key] = value
}
get(value){
return this.item[key]
}
remove(key){
delete this.item[key]
}
getKeys(){
return Object.keys(this.item)
}
getValues(){
// return Object.values(this.item)
// 或者
Object.getKeys(this.item).reduce((arr,index,k) =>{
arr.push(this.item[index])
return r
},[])
}
}