--- 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).