class Dictionary {
constructor() {
this.table = {}; // 存儲字典中的元素
}
}
複製代碼
toStrFn (key){
if (key === null) {
return 'NULL';
} else if (key === undefined) {
return 'UNDEFINED';
} else if (typeof key === 'string' || key instanceof String) {
return `${key}`;
}else if ( Object.prototype.toString.call({})==='[object Object'] ){
return JSON.stringify(obj)
}
return key.toString();
}
複製代碼
hasKey(key) { // 檢查字典中是否存在某個相同的key,存在返回true,反之false
return this.table[this.toStrFn(key)] != null;
}
複製代碼
class ValuePair { // 此類用來存儲table對象上的key屬性
constructor(key, value) {
this.key = key;
this.value = value;
}
toString() {
return `[#${this.key}: ${this.value}]`;
}
}
set(key, value) {
if (key != null && value != null) {
const tableKey = this.toStrFn(key); // 獲取表示key的字符串
this.table[tableKey] = new ValuePair(key, value); // 實例化ValuePair類。
return true;
}
return false;
}
複製代碼
remove(key) {
if (this.hasKey(key)) { // 若是字典中找到相同的key
delete this.table[this.toStrFn(key)]; // 刪除之
return true; // 若是能刪返回true
}
return false; // 不然返回false
}
複製代碼
get(key) {
const valuePair = this.table[this.toStrFn(key)];
return valuePair == null ? undefined : valuePair.value;
}
複製代碼
keyValues() {
return Object.values(this.table); // 執行Object類內置的values方法(ECMAScript2017)
}
複製代碼
keys() {
return this.keyValues().map(valuePair => valuePair.key); // 嗯~ 沒啥好解釋的
}
複製代碼
values() {
return this.keyValues().map(valuePair => valuePair.value);
}
複製代碼
forEach(callbackFn) {
const valuePairs = this.keyValues(); // 獲取全部valuePairs構成的數組
for (let i = 0; i < valuePairs.length; i++) { // 迭代每一個valueParir
const result = callbackFn(valuePairs[i].key, valuePairs[i].value); // 執行以參數形式傳入forEach方法的callbackFn函數
if (result === false) {// 若是回調參數返回false,會中斷forEatch方法的執行,打斷正在迭代valuePairs的for循環
break;
}
}
}
複製代碼
isEmpty() { // 字典是否爲空
return this.size() === 0;
}
size() { // 返回字典所包含的數量
return Object.keys(this.table).length;
}
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;
}
複製代碼
const dictionary = new Dictionary();
dictionary.set('Gandalf', 'gandalf@email.com'); // 添加
dictionary.set('John', 'johnsnow@email.com');// 添加
dictionary.set('Tyrion', 'tyrion@email.com');// 添加
console.log(dictionary.hasKey('Gandalf')); // true
console.log(dictionary.size()); // 3
console.log(dictionary.keys()); // ["Gandalf", "John", "Tyrion"]
console.log(dictionary.values()); // ["gandalf@email.com", "johnsnow@email.com", "tyrion@email.com"]
console.log(dictionary.get('Tyrion')); // tyrion@email.com
dictionary.remove('John');
console.log(dictionary.keys()); // ["Gandalf", "Tyrion"]
console.log(dictionary.values()); // ["gandalf@email.com", "tyrion@email.com"]
console.log(dictionary.keyValues()); // [{key: "Gandalf", value: "gandalf@email.com"}, {key: "Tyrion", value: "tyrion@email.com"}]
console.log(dictionary.toString()); // [#Gandalf: gandalf@email.com],[#Tyrion: tyrion@email.com]
dictionary.forEach((k, v) => {
console.log('forEach: ', `key: ${k}, value: ${v}`);
});
// forEach: key: Gandalf, value: gandalf@email.com
// forEach: key: Tyrion, value: tyrion@email.com
複製代碼
本文內容來自本菜閱讀《Javascript數據結構與算法》第三版第八章後,整理抄錄。碼字不易,若是以爲還能夠,歡迎各位點贊!!!javascript