/*
在字典中,存储的是[键,值]
对,其中键名是用来查询特定元素的。字典和集合很相似,集合以[值,值]的形式存储元素,字
典则是以[键,值]的形式来存储元素。字典也称作映射、符号表或关联数组
*/
/*
set(key,value):向字典中添加新元素。如果 key 已经存在,那么已存在的 value 会
被新的值覆盖。
remove(key):通过使用键值作为参数来从字典中移除键值对应的数据值。
hasKey(key):如果某个键值存在于该字典中,返回 true,否则返回 false。
get(key):通过以键值作为参数查找特定的数值并返回。
clear():删除该字典中的所有值。
size():返回字典所包含值的数量。与数组的 length 属性类似。
isEmpty():在 size 等于零的时候返回 true,否则返回 false。
keys():将字典所包含的所有键名以数组形式返回。
values():将字典所包含的所有数值以数组形式返回。
keyValues():将字典中所有[键,值]对返回。
forEach(callbackFn):迭代字典中所有的键值对。callbackFn 有两个参数:key 和
value。该方法可以在回调函数返回 false 时被中止(和 Array 类中的 every 方法相似)。
*/
/*
在字典中,理想的情况是用字符串作为键名,值可以是任何类型(从数、字符串等原始类型,
到复杂的对象)
*/
function defaultToString(item) {
if (item === null) {
return 'NULL'
}else if (item === undefined) {
return 'UNDEFINED'
} else if (typeof item === 'string' || item instanceof String) {
return `${item}`
}
return item.toString();
}
/*
为了在字典中保存 value,我们将 key 转化为了字符串,而为了保存信息的需要,我们同
样要保存原始的 key。
*/
class ValuePair {
constructor(key, value) {
this.key = key;
this.value = value;
}
toString() {
return `${this.key}: ${this.value}`
}
}
// 创建字典
class Dictionary {
constructor(toStrFn = defaultToString) {
this.toStrFn = toStrFn;
this.table = {};
}
// 检测一个键是否存在于字典中
/*
如果传入一个复杂对象作为键,
需要将它转化为一个字符串。因此我们需要调用 toStrFn 函数
*/
hasKey(key) {
return this.table[this.toStrFn(key)] != null;
}
// 在字典和 ValuePair 类中设置键和值
set(key, value) {
if (key != null && value != null) {
const tableKey = this.toStrFn(key)
this.table[tableKey] = new ValuePair(key, value);
return true;
}
return false;
}
// 从字典中移除一个值
remove(key) {
if(this.hasKey(key)) {
delete this.table[this.toStrFn(key)];
return true;
}
return false;
}
// 从字典中检索一个值
get(key) {
const valuePair = this.table[this.toStrFn(key)];
return valuePair == null ? undefined : valuePair.value;
}
// 以数组形式返回字典中的所有 valuePair 对象
keyValues() {
return Object.values(this.table);
}
// 可能不是所有浏览器都支持 Object.values 方法,我们也可以用下面的代码来代替
keyValuesTwo() {
const valuesPairs = [];
for(const k in this.table) {
if (this.hasKey(k)) {
valuesPairs.push(this.table[k]);
}
}
return valuesPairs;
}
//返回 Dictionary 类中用于识别值的所有(原始)键名
keys() {
return this.keyValues().map(valuePair => valuePair.key);
/*
方法二
*/
// const keys = [];
// const valuePairs = this.keyValues();
// for (let i = 0; i < valuePairs.length; i++) {
// keys.push(valuePairs[i].key);
// }
// return keys;
}
// 返回的是 value 属性
values() {
return this.keyValues().map(valuePair => valuePair.value);
}
// 用 forEach 迭代字典中的每个键值对
//callback迭代每个 valuePair(行{2})并执行以参数形式传入 forEach 方法的 callbackFn 函数(行{3}),保
// 存它的结果
forEach(callbackFn) {
const valuePairs = this.keyValues();
for (let i = 0; i < valuePairs.length; i++) { // {2}
const result = callbackFn(valuePairs[i].key, valuePairs[i].value); // {3}
if (result === false) {
break;
}
}
}
// size 方法返回字典中的值的个数
size() {
return Object.keys(this.table).length;
}
// 要检验字典是否为空
isEmpty() {
return this.size() === 0;
}
// 清空字典内容
clear() {
this.table = {};
}
toString() {
if (this.isEmpty()) {
return ''
}
const valuePairs = this.keyValues();
let objString = `${valuePairs[0].toString()}`;
for (let i = 1; i < valuePairs.length; i++) {
objString = `${objString},${valuePairs[i].toString()}`;
}
return objString;
}
}
let dictionary = new Dictionary();
dictionary.set(1,'lal')
dictionary.set(2,'ddd')
dictionary.set('three',3)
console.log(dictionary);
let key_value = dictionary.keyValues();
console.log(key_value);
dictionary.forEach((k, v) => {
console.log('forEach:', `key:${k}, value: ${v}`);
})