9609c04755
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더). - Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외) - Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체) - Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속) 각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
229 lines
8.0 KiB
Markdown
229 lines
8.0 KiB
Markdown
---
|
|
id: javascript-map-methods
|
|
title: "JavaScript Map Methods"
|
|
category: "Frontend"
|
|
status: "draft"
|
|
verification_status: "conceptual"
|
|
canonical_id: ""
|
|
aliases: ["Map methods", "Map.set", "Map.get", "Map.has", "Map.size", "Map.groupBy", "objects as keys"]
|
|
duplicate_of: ""
|
|
source_trust_level: "B"
|
|
confidence_score: 0.9
|
|
created_at: 2026-06-23
|
|
updated_at: 2026-06-23
|
|
review_reason: ""
|
|
merge_history: []
|
|
tags: ["javascript", "js", "web", "frontend", "w3schools", "map", "methods"]
|
|
raw_sources: ["https://www.w3schools.com/js/js_map_methods.asp"]
|
|
applied_in: []
|
|
github_commit: ""
|
|
---
|
|
|
|
# [[JavaScript Map Methods]]
|
|
|
|
## 🎯 한 줄 통찰 (One-line insight)
|
|
Map methods — `set`, `get`, `has`, `delete`, `clear`, `forEach`, `entries`, `keys`, `values`, plus `size` — manage and iterate key-value pairs, and crucially Maps can use objects as keys; ES2024 also adds the static `Map.groupBy()`. [S1]
|
|
|
|
## 🧠 핵심 개념 (Core concepts)
|
|
- **`get()` retrieves**, **`set()` adds or updates** a key's value, **`has()` tests** key existence, **`delete()` removes** one element, **`clear()` empties** the Map, **`size`** returns the count. [S1]
|
|
- **Three iteration methods** — `forEach()` (callback per pair), `entries()` (`[key, value]` iterator), `keys()` and `values()` (key/value iterators). [S1]
|
|
- **Objects can be Map keys** — an important Map feature; a string key won't match an object key, so `get("apples")` returns `undefined` when the key is an object. [S1]
|
|
- **`Map.groupBy()` (ES2024)** groups elements of an iterable by a callback's return value, producing a Map. (`Object.groupBy()` groups into an object; `Map.groupBy()` groups into a Map.) [S1]
|
|
|
|
## 🧩 추출된 패턴 (Extracted patterns)
|
|
- **Iterate-and-accumulate** — `for...of` over `entries()`, `keys()`, or `values()` to build text or sum totals. [S1]
|
|
- **delete-then-check** — `delete()` followed by `has()` to confirm removal. [S1]
|
|
- **Object keys for identity** — store per-object data by using the object itself as the key. [S1]
|
|
- **Group by predicate** — `Map.groupBy(iterable, callback)` to bucket items. [S1]
|
|
|
|
## 📖 세부 내용 (Details)
|
|
**The get() Method** — gets the value of a key in a Map: [S1]
|
|
```javascript
|
|
// Create a Map
|
|
const fruits = new Map([
|
|
["apples", 500],
|
|
["bananas", 300],
|
|
["oranges", 200]
|
|
]);
|
|
```
|
|
```javascript
|
|
fruits.get("apples");
|
|
```
|
|
|
|
**The set() Method** — adds or changes the value of a key: [S1]
|
|
```javascript
|
|
// Create a Map
|
|
const fruits = new Map();
|
|
|
|
// Set Map Values
|
|
fruits.set("apples", 500);
|
|
fruits.set("bananas", 300);
|
|
fruits.set("oranges", 200);
|
|
```
|
|
```javascript
|
|
fruits.set("apples", 500);
|
|
```
|
|
|
|
**The size Property** — returns the number of elements: [S1]
|
|
```javascript
|
|
fruits.size;
|
|
```
|
|
|
|
**The delete() Method** — removes a Map element specified by a key: [S1]
|
|
```javascript
|
|
fruits.delete("apples");
|
|
```
|
|
|
|
**The clear() Method** — removes all elements: [S1]
|
|
```javascript
|
|
fruits.clear();
|
|
```
|
|
|
|
**The has() Method** — returns `true` if a key exists: [S1]
|
|
```javascript
|
|
fruits.has("apples");
|
|
```
|
|
```javascript
|
|
fruits.delete("apples");
|
|
fruits.has("apples");
|
|
```
|
|
|
|
**The forEach() Method** — invokes a callback for each key/value pair: [S1]
|
|
```javascript
|
|
// List all entries
|
|
let text = "";
|
|
fruits.forEach (function(value, key) {
|
|
text += key + ' = ' + value;
|
|
})
|
|
```
|
|
|
|
**The entries() Method** — returns an iterator with the `[key, value]` pairs: [S1]
|
|
```javascript
|
|
// List all entries
|
|
let text = "";
|
|
for (const x of fruits.entries()) {
|
|
text += x;
|
|
}
|
|
```
|
|
|
|
**The keys() Method** — returns an iterator with the keys: [S1]
|
|
```javascript
|
|
// List all keys
|
|
let text = "";
|
|
for (const x of fruits.keys()) {
|
|
text += x;
|
|
}
|
|
```
|
|
|
|
**The values() Method** — returns an iterator with the values: [S1]
|
|
```javascript
|
|
// List all values
|
|
let text = "";
|
|
for (const x of fruits.values()) {
|
|
text += x;
|
|
}
|
|
```
|
|
You can use the `values()` method to sum the values in a Map: [S1]
|
|
```javascript
|
|
// Sum all values
|
|
let total = 0;
|
|
for (const x of fruits.values()) {
|
|
total += x;
|
|
}
|
|
```
|
|
|
|
**Objects as Keys** — being able to use objects as keys is an important Map feature: [S1]
|
|
```javascript
|
|
// Create Objects
|
|
const apples = {name: 'Apples'};
|
|
const bananas = {name: 'Bananas'};
|
|
const oranges = {name: 'Oranges'};
|
|
|
|
// Create a Map
|
|
const fruits = new Map();
|
|
|
|
// Add new Elements to the Map
|
|
fruits.set(apples, 500);
|
|
fruits.set(bananas, 300);
|
|
fruits.set(oranges, 200);
|
|
```
|
|
Remember: the key is an object (`apples`), not a string (`"apples"`): [S1]
|
|
```javascript
|
|
fruits.get("apples"); // Returns undefined
|
|
```
|
|
|
|
**JavaScript Map.groupBy()** — the ES2024 `Map.groupBy()` method groups elements of an iterable into a Map according to a callback's return value. The difference between `Object.groupBy()` and `Map.groupBy()` is output type: the former groups elements into a JavaScript object, the latter into a Map object: [S1]
|
|
```javascript
|
|
// Create an Array
|
|
const fruits = [
|
|
{name:"apples", quantity:300},
|
|
{name:"bananas", quantity:500},
|
|
{name:"oranges", quantity:200},
|
|
{name:"kiwi", quantity:150}
|
|
];
|
|
|
|
// Callback function to Group Elements
|
|
function myCallback({ quantity }) {
|
|
return quantity > 200 ? "ok" : "low";
|
|
}
|
|
|
|
// Group by Quantity
|
|
const result = Map.groupBy(fruits, myCallback);
|
|
```
|
|
|
|
**Map Methods and Properties** [S1]
|
|
|
|
| Method | Description |
|
|
|--------|-------------|
|
|
| new Map() | Creates a new Map |
|
|
| set() | Sets the value for a key in a Map |
|
|
| get() | Gets the value for a key in a Map |
|
|
| has() | Returns true if a key exists in a Map |
|
|
| delete() | Removes a Map element specified by a key |
|
|
| clear() | Removes all the elements from a Map |
|
|
| forEach() | Calls a function for each key/value pair in a Map |
|
|
| entries() | Returns an iterator object with the [key, value] pairs in a Map |
|
|
| keys() | Returns an iterator object with the keys in a Map |
|
|
| values() | Returns an iterator object of the values in a Map |
|
|
| size | Returns the number of elements in a Map |
|
|
|
|
## 🛠️ 적용 사례 (Applied in summary)
|
|
The page's own snippets are the canonical applied examples — building a `fruits` Map, reading `size`, removing/checking with `delete()`+`has()`, iterating via `forEach()`/`entries()`/`keys()`/`values()`, summing values, using objects as keys, and grouping an array with `Map.groupBy()`. No external project/commit applications found in the source.
|
|
|
|
## 💻 코드 패턴 (Code patterns)
|
|
Sum all values in a Map (language: JavaScript):
|
|
```javascript
|
|
let total = 0;
|
|
for (const x of fruits.values()) {
|
|
total += x;
|
|
}
|
|
```
|
|
Group an array into a Map by a callback:
|
|
```javascript
|
|
const result = Map.groupBy(fruits, myCallback);
|
|
```
|
|
|
|
## ⚖️ 비교 및 선택 기준 (Comparison & decision criteria)
|
|
Both `Object.groupBy()` and `Map.groupBy()` group an iterable by a callback's return value; choose by desired output type: `Object.groupBy()` produces a plain JavaScript object, while `Map.groupBy()` produces a Map object. [S1]
|
|
|
|
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
|
`Map.groupBy()` is an ECMAScript 2024 addition, so older environments may need a polyfill. A common pitfall noted by the source: object keys are matched by identity, so `get("apples")` (a string) returns `undefined` when the key was the object `apples`. No contradictions found in the source. [S1]
|
|
|
|
## ✅ 검증 상태 및 신뢰도
|
|
- **상태:** draft
|
|
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
|
|
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
|
- **신뢰 점수:** 0.90
|
|
- **중복 검사 결과:** 신규 생성 (New discovery)
|
|
|
|
## 🔗 지식 그래프 (Knowledge Graph)
|
|
- **상위/루트:** [[JavaScript Tutorial]]
|
|
- **관련 개념:** [[JavaScript Maps]], [[JavaScript Set Methods]], [[JavaScript Sets]]
|
|
- **참조 맥락:** Referenced whenever managing, iterating, or grouping the contents of a Map.
|
|
|
|
## 📚 출처 (Sources)
|
|
- [S1] W3Schools — JavaScript Map Methods — https://www.w3schools.com/js/js_map_methods.asp
|
|
|
|
## 📝 변경 이력 (Change history)
|
|
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Map Methods" page (Astra wiki-curation, P-Reinforce v3.1 format).
|