JS中数据结构之字典
2024-10-07 14:28:58
字典是一种以键 - 值对形式存储数据的数据结构
通过数组实现字典
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];
}
}
最新文章
- h5嵌入视频遇到的bug及总结
- bash shell,调用ffmpeg定期截图
- C阶段【02】 - 分支结构
- matlab生成HEX文件-任意信号 大于64K长度
- 广搜+输出路径 POJ 3414 Pots
- 慢牛APP相关截图
- 初学Ajax(二)
- Android HTTPS(4)直接用SSLSocket,黑名单,客户端证书
- [React Testing] Reusing test boilerplate
- sqlserver系统表操作
- A fatal error has been detected by the Java Runtime Environment:
- 关于android混淆文件project.properties和proguard-project.txt详解
- SSM-SpringMVC-30:SpringMVC中InitBinder的骇客级优化
- Go 语言之三驾马车
- ScheduledThreadPoolExecutor Usage
- oracle drop 表后 恢复
- [转帖]TMD为你揭秘中国互联网下半场所有秘密
- Sequence 加速
- R-table和tapply函数
- 502 解决:[WARNING] fpm_children_bury