Front/JS & jQuery

[js]for (const [typeTp, items] of Object.entries(grouped))

오선지♬ 2025. 9. 17. 09:09
728x90
반응형

1. Object.entries(grouped)

  • Object.entries() 는 객체를 [key, value] 쌍의 배열로 바꿔줍니다.
    예를 들어:
const grouped = {
  "1": [{ id: 1 }, { id: 2 }],
  "2": [{ id: 3 }]
};

console.log(Object.entries(grouped));

출력:

[
  ["1", [{ id: 1 }, { id: 2 }]],
  ["2", [{ id: 3 }]]
]

즉, grouped 객체가 { key: value } 구조라면
Object.entries(grouped) → [ [key1, value1], [key2, value2], ... ]


2. for (const [typeTp, items] of ...)

  • for...of 구문은 배열을 순회할 때 사용합니다.
  • Object.entries(grouped)의 각 요소는 [key, value] 배열입니다.
  • 구조 분해 할당을 사용해서 [key, value] 를 각각 typeTp와 items 변수에 담는 겁니다.

예시:

 
for (const [typeTp, items] of Object.entries(grouped)) {
  console.log("key:", typeTp);   // 객체의 키
  console.log("value:", items); // 객체의 값
}

출력:

 
key: 1
value: [ { id: 1 }, { id: 2 } ]
key: 2
value: [ { id: 3 } ]

✅ 정리하면:
for (const [typeTp, items] of Object.entries(grouped))
→ grouped 객체의 각 속성(key, value)을 순회하면서 key는 typeTp, value는 items에 할당한다는 뜻입니다.

728x90
반응형

'Front > JS & jQuery' 카테고리의 다른 글

[js] toLocaleString  (0) 2025.09.15
[jQuery] insertAfter  (0) 2025.09.12
[jQuery] insertBefore  (0) 2025.09.11
[js] capitalizeFirstLetter  (0) 2025.09.04
[js] String.prototype  (0) 2025.09.03