← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 9 of 26

JS मैप्स

किसी coat check counter के बारे में सोचें, आप अपना coat देते हैं और एक numbered ticket लेते हैं, वह ticket, यानी key, बाद में आपका ठीक वही coat, यानी value, भरोसेमंद तरीके से वापस दिलाता है, चाहे coats किसी भी क्रम में आए हों। JavaScript में एक Map वही coat check system है, key-value pairs का एक collection जहाँ किसी भी type की value key हो सकती है, सिर्फ text नहीं, और किसी key से value ढूंढना तेज़ और भरोसेमंद होता है। Maps structured lookups के लिए स्वाभाविक रूप से फिट बैठते हैं, जैसे cookiescursor.com पर किसी visitor की per-tutorial progress को track करना, जो tutorial की अपनी identifier से keyed होती है।
Syntax
javascript
const mapName = new Map();
mapName.set(key, value);
mapName.get(key);
mapName.has(key);

Map बनाना

एक Map new Map() से बनाया जाता है, चाहें तो इसे [key, value] pairs की एक array से शुरू किया जा सकता है, जिससे आपको शुरू से ही key से lookup के लिए तैयार एक structured collection मिल जाता है।

Note: किसी Map को सीधे pairs की array से शुरू करना अक्सर एक खाली Map बनाकर बार-बार set() कॉल करने से ज़्यादा साफ होता है।
Warning: plain object के उलट, किसी Map की keys अपने आप strings में नहीं बदलतीं, numeric key 1 string key 1 से अलग ही रहती है।

उदाहरण: Creating a Map

javascript
const map = new Map([["name", "Sam"], ["age", 30]]);
console.log(map);

set और get

set(key, value) Map में एक entry जोड़ता या update करता है, और get(key) किसी दी गई key के नीचे store की गई value लाता है, और अगर वह key मौजूद न हो तो undefined return करता है।

दोनों methods किसी भी value को key के रूप में स्वीकार करते हैं, objects और functions सहित, सिर्फ strings नहीं।

Note: पहले से मौजूद key के साथ set() कॉल करना बस उस entry की value को update कर देता है, यह कोई duplicate entry नहीं बनाता।
Warning: किसी गायब key पर get() error देने के बजाय undefined return करता है, अगर key के मौजूद होने की पक्की जानकारी न हो तो पहले has() से जांच लें।

उदाहरण: set and get

javascript
// Declare the constant `map` as a new `Map` instance
// Declare the constant `map` as a new `Map` instance
const map = new Map();
// Call `map.set("name", "Alex")`
// Call `map.set("name", "Alex")`
map.set("name", "Alex");
// Print `map.get("name")` to the console
// Print `map.get("name")` to the console
console.log(map.get("name"));
// Print `map.get("missing")` to the console
// Print `map.get("missing")` to the console
console.log(map.get("missing"));

has, delete, और size

has(key) जांचता है कि किसी Map में कोई खास key है या नहीं, delete(key) किसी entry को पूरी तरह हटा देता है, और size property बताती है कि Map में फिलहाल कितनी entries हैं।

get() से पहले has() जांचना यह पहचानने का एक आम तरीका है कि कोई key सच में गायब है या उसकी value महज undefined है।

Note: get() से पहले has() जांचना 'key missing' और 'key present but its value is undefined' के बीच फर्क करने का एक साफ, explicit तरीका है।
Warning: delete() सिर्फ तभी true return करता है जब key असल में मौजूद थी और हटाई गई, किसी nonexistent key पर इसे कॉल करने पर बिना error के false मिलता है।

उदाहरण: has, delete, and size

javascript
// Declare the constant `map` as a new `Map` instance
// Declare the constant `map` as a new `Map` instance
const map = new Map([["a", 1]]);
// Print `map.has("a")` to the console
// Print `map.has("a")` to the console
console.log(map.has("a"));
// Call `map.delete("a")`
// Call `map.delete("a")`
map.delete("a");
// Print `map.size` to the console
// Print `map.size` to the console
console.log(map.size);

Map को Iterate करना

Maps iterable होते हैं, और array destructuring के साथ for...of, for (let [key, value] of myMap), insertion order में हर key-value pair तक पहुंचने का सबसे आम और readable तरीका है।

Note: जब आपको हर pair का सिर्फ एक हिस्सा चाहिए हो, तो हर बार दोनों destructure करने के बजाय सिर्फ myMap.keys() या myMap.values() इस्तेमाल करें।
Warning: Sets की तरह, Maps भी insertion order बनाए रखते हैं, लेकिन अगर आप किसी key को delete करके दोबारा जोड़ें, तो वह order बदल जाता है, वह key अंत में चली जाती है।

उदाहरण: Iterating a Map

javascript
// Declare the constant `map` as a new `Map` instance
// Declare the constant `map` as a new `Map` instance
const map = new Map([["a", 1], ["b", 2]]);
// Classic for-loop: `let [key, value] of map`
// Classic for-loop: `let [key, value] of map`
for (let [key, value] of map) {
  // Print `key, value` to the console
  // Print `key, value` to the console
  console.log(key, value);
}

Map बनाम Object

एक Map किसी भी type की key स्वीकार करता है, अपनी size सीधे track करता है, और भरोसेमंद insertion order बनाए रखता है, जबकि सीधे string-keyed data के लिए एक plain object अक्सर सरल होता है और जाने-पहचाने dot और bracket notation का फायदा देता है।

Note:
  • Map का इस्तेमाल करें जब:
  • keys साधारण strings न हों
  • आपको बार-बार additions और deletions चाहिए हों
  • आपको एक built-in size property चाहिए।
Warning: एक plain object में कोई built-in size property नहीं होती, और ऐतिहासिक रूप से सभी engines में key order की भी गारंटी नहीं होती, खासतौर पर इन features पर निर्भर रहना Map इस्तेमाल करने के पक्ष में जाता है।

उदाहरण: Map vs Object

javascript
// Declare the constant `map` as a new `Map` instance
// Declare the constant `map` as a new `Map` instance
const map = new Map();
// Call `map.set(1, "numeric key")`
// Call `map.set(1, "numeric key")`
map.set(1, "numeric key");
// Print `map.size` to the console
// Print `map.size` to the console
console.log(map.size);
// Declare the constant `obj`, set to `{ name: "Sam" }`
// Declare the constant `obj`, set to `{ name: "Sam" }`
const obj = { name: "Sam" };
// Print `obj.name` to the console
// Print `obj.name` to the console
console.log(obj.name);
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. bracket notation इस्तेमाल करना, जैसे myMap[key], उचित get() और set() methods के बजाय, Maps bracket access support नहीं करते।
  2. यह भूल जाना कि किसी Map का size size property से check होता है, Set की तरह, array की length से नहीं।
  3. Map को plain object से confuse करना, Maps किसी भी type की key allow करते हैं, objects और numbers समेत, जबकि plain object keys हमेशा strings में convert हो जाती हैं।
चैप्टर सारांश
  • एक Map key-value pairs store करता है जहाँ keys किसी भी type की हो सकती हैं, सिर्फ strings नहीं।
  • set(), get(), delete(), और has() किसी Map के contents manage करने के मुख्य methods हैं।
  • Maps को for...of से iterate किया जा सकता है, और उनकी size property बताती है कि उनमें कितनी entries हैं।
ब्राउज़र सपोर्ट

Map को 2015 से सभी major browsers में support मिला है और यह modern JavaScript का पूरी तरह standard हिस्सा है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.