您的位置:首页 > 理论基础 > 数据结构算法

数据结构 - 字典

2019-06-15 09:49 1206 查看

字典

数据结构 - 字典,可重复。

// 字典实现
// ES5
var Dictionary = function() {
var items = {};

// 检查键是否存在
this.has = function(key) {
// return items.hasOwnProperty(key);
return key in items;
};

// 添加元素
this.set = function(key, value) {
items[key] = value;
};

// 删除元素
this.delete = function(key) {
if (this.has(key)) {
delete items[key];
return true;
}
return false;
};

// 获取值
this.get = function(key) {
if (this.has(key)) {
return items[key];
}
return undefined;
};

// 获取全部键
this.keys = function() {
return Object.keys(items);
};

// 清除字典
this.clear = function() {
items = {};
};

// 获取字典长度
this.size = function() {
return Object.keys(items).length;
};

// 检查items
this.getItem = function() {
return items;
};
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: