티스토리 뷰

딕셔너리 (Dictionary)

  • key-value 형태로 다양한 자료형 개체(Entity)를 저장하는 자료구조(~Map)
  • 구현 메서드 (method)
    • 전체 개체 획득/ 초기화 / 크기 반환: Dictionary.getBuffer(), Dictionary.clear(). Dictionary.size()
    • 개체 추가/삭제/반환/여부: Dictionary.set(), Dictionary.remove(), Dictionary.get(), Dictionary.has()
    • key, value 배열 반환/ 고차 함수: Dictionary.keys(), Dictionary.values(), Dictionary.each()

image

 

📌 딕셔너리 (Dictionary) 구현  소스


// Dictionary(): 개체(Entity)를 저장할 생성자
function Dictionary(items = {}) {
  this.items = items;
}

// 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(): 개체 존재 여부 확인
Dictionary.prototype.has = function (key) {
  // return value in this.items;
  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.prototype.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;
  return Object.values(this.items);
};

Dictionary.prototype.each = function (fn) {
  for (let k in this.items) {
    fn(k, this.items[k]);
  }
};

function printDictionary(key, value) {
  console.log(`key: ${key}`);
  console.log(`value: ${value}`);
}

let dict = new Dictionary();
dict.set("age", 19);
dict.set("name", "alice");
dict.set("height", 172);
console.log(dict);

dict.remove("age");
console.log(dict);

console.log(dict.has("age"));
console.log(dict.has("name"));
console.log(dict.get("age"));
console.log(dict.get("name"));

console.log(dict.keys());
console.log(dict.values());
dict.each(printDictionary);
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/04   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
글 보관함