使用 js 实现 字典

本文介绍了如何用JavaScript的Object类创建字典类,包括add、find、remove等操作,以及getKeys、sort、count和显示功能,通过电话薄案例展示了其实用性。

字典是一种以 键值对 形式存数数据的数据结构,javaScript 中的 Object 类就是以字典的形式设置的,所以使用 Object 类本身的特性实现字典 Dictionary 类。

属性及方法

列表属性或方法描述
add方法add(key, value) 向字典中添加 键值对
find方法find(key) 从字典中查找 key 键对应的值,找不到则返回 undefined
remove方法remove(key) 从字典中删除 key
getKeys方法getKeys() 获取字典中的所有 key
getKeysSort方法getKeysSort() 获取字典中的所有 key,并使用 sort 排序
count方法count() 返回字典中 键值对 的个数
showAll方法showAll() 将字典中的所有 键值对 转换为字符串,以 , 分隔
showAllSort方法showAllSort() 将字典中的所有键按照 sort 排序,之后将所有 键值对 转换为字符串,以 , 分隔
clear方法clear() 清空字典

字典代码实现

class Dictionary {
  constructor() {
    this.dataStore = {}
  }

  add(key, value) {
    this.dataStore[key] = value
  }

  find(key) {
    return this.dataStore[key]
  }

  remove(key) {
    delete this.dataStore[key]
  }

  getKeys() {
    return Object.keys(this.dataStore)
  }

  getKeysSort() {
    return this.getKeys().sort()
  }

  count() {
    return this.getKeys().length
  }

  showAll() {
    let res = ''
    this.getKeys().forEach(key => {
      res += `,{key:'${key}',value:'${this.dataStore[key]}'}`
    })
    return res.slice(1)
  }

  showAllSort() {
    let res = ''
    this.getKeys().sort().forEach(key => {
      res += `,{key:'${key}',value:'${this.dataStore[key]}'}`
    })
    return res.slice(1)
  }
  
  clear() {
    this.getKeys().forEach(key => {
      delete this.dataStore[key]
    })
    this.dataStore = {}
  }
}

字典的使用

常见实现比如说电话薄,电话薄利的名字相当于键,电话号码相当于值,组合在一起就是键值对。一般查找电话号码是依据名字查找。电话薄的排序也是依据名字进行排序,下面展示电话薄的简单示例:

const phoneDictionary = new Dictionary()
const phoneDictionary1 = new Dictionary()
phoneDictionary.add('yi', 13010102020)
phoneDictionary.add('er', 13030304040)
phoneDictionary1.add('yi', 13010102020)
phoneDictionary1.add('er', 13030304040)
phoneDictionary.add('zhangsan', 13000001111)
phoneDictionary.add('lisi', 13022223333)
phoneDictionary.add('wangwu', 13044445555)
phoneDictionary.add('luliu', 13066667777)
phoneDictionary.add('zhaoqi', 13088889999)
console.log(phoneDictionary)
console.log(`phoneDictionary.find('zhangsan')`, phoneDictionary.find('zhangsan'))
console.log(`phoneDictionary.find('zhangsan1')`, phoneDictionary.find('zhangsan1'))
phoneDictionary.remove('yi')
console.log(phoneDictionary)
console.log(`phoneDictionary.getKeys()`, phoneDictionary.getKeys())
console.log(`phoneDictionary.getKeysSort()`, phoneDictionary.getKeysSort())
console.log(`phoneDictionary.count()`, phoneDictionary.count())
console.log(`phoneDictionary.showAll()`, phoneDictionary.showAll())
console.log(`phoneDictionary.showAllSort()`, phoneDictionary.showAllSort())
console.log(phoneDictionary1)

感谢

本次分享到这里就结束了,感谢您的阅读!如对您有帮助,帮忙点个赞,您的点赞是我继续创作的动力。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值