티스토리 뷰

5.9 Object.keys, values, entries

위 함수들의 결과값은 실제 배열로 반환된다.

Transforming objects

Objects는 배열로써 처리하는 함수들이 존재하지 않아서, 관련처리를 하고 싶으면 아래와 같이 해야한다. (잘 이해되진 않는다...)

let prices = {
  banana: 1,
  orange: 2,
  meat: 4,
};

let doublePrices = Object.fromEntries(
  // convert prices to array, map each key/value pair into another pair
  // and then fromEntries gives back the object
  Object.entries(prices).map(entry => [entry[0], entry[1] * 2])
);

alert(doublePrices.meat); // 8
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday