字典是一种以键 - 值对形式存储数据的数据结构

通过数组实现字典

function Dictionary() {
this.add = add;
this.datastore = new Array();
this.find = find;
this.remove = remove;
this.showAll = showAll;
this.count = count;
this.clear = clear;
}

add() 方法接受两个参数:键和值

function add(key, value) {
  this.datastore[key] = value;
}

find() 方法以键作为参数,返回和其关联的值

function find(key) {
  return this.datastore[key];
}

remove() 方法从字典中删除键 - 值对

function remove(key) {
  delete this.datastore[key];
}

showAll() 方法显示字典中所有的键 - 值对

function showAll() {
for (var key in this.datastore) {
console.log(key + " -> " + this.datastore[key]);
}
}

count() 方法显示字典中的元素个数

function count() {
var n = 0;
for (var key in this.datastore) {
++n;
}
return n;
}

clear() 方法清空键值对

function clear() {
for(var key in this.datastore) {
delete this.datastore[key];
}
}

最新文章

  1. h5嵌入视频遇到的bug及总结
  2. bash shell,调用ffmpeg定期截图
  3. C阶段【02】 - 分支结构
  4. matlab生成HEX文件-任意信号 大于64K长度
  5. 广搜+输出路径 POJ 3414 Pots
  6. 慢牛APP相关截图
  7. 初学Ajax(二)
  8. Android HTTPS(4)直接用SSLSocket,黑名单,客户端证书
  9. [React Testing] Reusing test boilerplate
  10. sqlserver系统表操作
  11. A fatal error has been detected by the Java Runtime Environment:
  12. 关于android混淆文件project.properties和proguard-project.txt详解
  13. SSM-SpringMVC-30:SpringMVC中InitBinder的骇客级优化
  14. Go 语言之三驾马车
  15. ScheduledThreadPoolExecutor Usage
  16. oracle drop 表后 恢复
  17. [转帖]TMD为你揭秘中国互联网下半场所有秘密
  18. Sequence 加速
  19. R-table和tapply函数
  20. 502 解决:[WARNING] fpm_children_bury

热门文章

  1. 【CDN+】 Hbase入门 以及Hbase shell基础命令
  2. 为什么每次打出的包都是Release版本呢?
  3. 实验吧 Web的WriteUp
  4. mybatis多对多
  5. python的正则
  6. phpcms列表分页ajax加载更多
  7. jvm性能监控(2)–JVM的监控工具jstat
  8. 【五一qbxt】day7-2 选择客栈
  9. git-ssh-keygen
  10. sql server 自优化