❓ 딕셔너리
- key-value 형태로 다양한 자료형 개체(Entity)를 저장하는 자료구조이다. (= Map과 동일)
function Dictionary(items = {}) {
this.items = items;
}
✏️ 구현 메서드(Method)
📗 getBuffer : 모든 개체(Entity)를 반환
Dictionary.prototype.getBuffer = function () {
return {...this.items};
};
📗 clear() : 초기화
Dictionary.prototype.clear = function () {
this.items = {};
};
📗 size() : 크기 반환
Dictionary.prototype.size = function () {
return Object.keys(this.items).length;
}
📗 has() : 개체 존재 여부 확인 (key 정보를 배열로 반환)
Dictionary.prototype.has = function () {
return this.items.hasOwnProperty(key);
};
📗 set() : 개체(Entity) 추가
Dictionary.prototype.set = function (key, value) {
this.items[key] = value;
};
📗 get() : 개체(Entity)의 value 반환
Dictionary.prototype.get = function (key) {
return this.has(key) ? this.items[key] : undefined;
}
📗 remove() : 개체(Entity) 삭제
Dictionary.prototype.remove = function (key) {
if(this.has(key)) {
delete this.items[key];
return true;
}
return false;
};
📗 keys() : 모든 key 값을 배열 형태로 반환
Dictionary.protoype.keys = function () {
return Object.keys(this.items);
};
📗 values() : 모든 value 값을 배열 형태로 반환
Dictionary.prototype.values = function () {
let values = [];
for(let k in this.items){
values.push(this.items[k]);
}
return values;
};
📗 printDictionary() : 딕셔너리 데이터 출력
function printDictionary(key, value){
console.log(`key : ${key}`);
console.log(`value : ${value}`);
};
📗 each() : 데이터 출력 함수 순회
Dictionary.prototype.each = function (fn){
for(let k in this.items){
fn(k, this.items[k]);
}
}