JS मैप्स
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 मिल जाता है।
उदाहरण: Creating a Map
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 नहीं।
उदाहरण: set and get
// 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 है।
उदाहरण: has, delete, and size
// 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 तरीका है।
उदाहरण: Iterating a Map
// 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 का फायदा देता है।
- Map का इस्तेमाल करें जब:
- keys साधारण strings न हों
- आपको बार-बार additions और deletions चाहिए हों
- आपको एक built-in size property चाहिए।
उदाहरण: Map vs Object
// 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);
- bracket notation इस्तेमाल करना, जैसे myMap[key], उचित get() और set() methods के बजाय, Maps bracket access support नहीं करते।
- यह भूल जाना कि किसी Map का size size property से check होता है, Set की तरह, array की length से नहीं।
- 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 हिस्सा है।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic