Skip to content
Arrays & ObjectsChapter 26 / 47Intermediate5 min read

Map & Set

JavaScript Map aur Set kya hain, object aur array se farq, unique values, kisi bhi type ki keys, Map.groupBy aur naye Set methods, Roman Urdu mein.

On this page

1. Map & Set kya hain?

Object ki keys sirf string ya symbol hoti hain. Map mein key number, object, hatta ke function bhi ho sakti hai, aur insertion order yaad rehta hai.

Set array jaisa hai lekin duplicate values allow nahi karta. Ek value ya to set mein hai ya nahi.

2. Kyun zaroori hai?

Objects aur arrays har kaam ke liye perfect nahi. Bar bar add/remove hone wale lookups aur unique lists ke liye Map aur Set behtar aur tez hain.

Object / array se kaam chalanaMap / Set ke saath
Object keys string ban jati hainMap mein key apni asal type mein
Size ke liye Object.keys().lengthmap.size seedha
Duplicate hatane ke liye filter aur indexOfnew Set se ek line mein unique
includes poori array scan karta haiset.has fast lookup

3. Kahan use hota hai?

SituationKya use karein
Unique tags, categories, visited IDsSet
Duplicate items hatana[...new Set(arr)]
User ID se user data cacheMap
DOM element se extra data jornaMap ya WeakMap
Bar bar add/delete hone wala lookupMap
Do lists ka common ya farqSet methods

4. Kaise kaam karta hai?

KaamMapSet
Banananew Map()new Set()
Addmap.set(key, value)set.add(value)
Parhnamap.get(key)(value hi key hai)
Checkmap.has(key)set.has(value)
Deletemap.delete(key)set.delete(value)
Countmap.sizeset.size
Loopfor (const [k, v] of map)for (const v of set)
Kab kya chunein
Sirf unique values chahiye?
Haan to Set
Key-value chahiye aur keys dynamic ya non-string?
Haan to Map
Fixed shape wala data, JSON mein bhejna hai?
Plain object

Map aur Set dono insertion order mein iterate hote hain. Lekin yeh seedha JSON.stringify mein convert nahi hote, pehle Object.fromEntries(map) ya [...set] karna parta hai.

5. Code example

Set: unique values aur set operations

js
const tags = ["js", "react", "js", "css", "react"];
const unique = new Set(tags);

console.log(unique.size);
console.log(unique.has("css"));
console.log([...unique]);

const frontend = new Set(["js", "css", "html"]);
const backend = new Set(["js", "sql"]);

console.log([...frontend.intersection(backend)]);
console.log([...frontend.union(backend)]);
console.log([...frontend.difference(backend)]);

Map: cache aur grouping

js
const userCache = new Map();
userCache.set(101, { name: "Ali" });
userCache.set(102, { name: "Sara" });

console.log(userCache.get(101).name);
console.log(userCache.has(103), userCache.size);

const orders = [
  { id: 1, status: "paid" },
  { id: 2, status: "pending" },
  { id: 3, status: "paid" },
];
const byStatus = Map.groupBy(orders, (o) => o.status);
console.log(byStatus.get("paid").length);

console.log(Object.fromEntries(userCache));

Code kaise kaam kar raha hai?

CheezMatlab
new Set(tags)Duplicates khud hat gaye, size 3
[...unique]Set ko wapas array banaya
intersectionDono mein common, ["js"]
unionSab unique, ["js", "css", "html", "sql"]
differenceSirf frontend wale, ["css", "html"]
userCache.set(101, ...)Number key apni type mein rehti hai
Map.groupByStatus ke hisaab se groups wala Map (ES2024)
Object.fromEntriesMap ko plain object banaya, JSON ke liye

Interview tip

WeakMap aur WeakSet sirf objects ko key/value bana sakte hain aur garbage collection ko nahi rokte. DOM elements ya objects ke saath private data jorne ke liye inhein use kiya jata hai.

6. Common mistakes

Galati: Map par bracket notation use karna

Why? map['key'] = value Map entry nahi banata, sirf object property lagata hai, get aur size mein nazar nahi aata.

Behtar: Hamesha map.set(key, value) aur map.get(key) use karo.

Galati: Set mein objects ko content se unique samajhna

Why? Do alag objects same data ke saath bhi alag references hain, Set dono rakh leta hai.

Behtar: Objects ke liye unique key (jaise id) ka Set ya Map banao.

Galati: Map ko seedha JSON.stringify karna

Why? JSON.stringify(map) ka result khaali object aata hai, data gum ho jata hai.

Behtar: Pehle Object.fromEntries(map) ya [...map] karo, phir stringify.

Galati: Har jagah Map zabardasti

Why? Fixed shape ka data jaise user profile plain object mein zyada saaf aur JSON friendly hai.

Behtar: Dynamic lookups ke liye Map, fixed record ke liye object.

7. Pehle aur baad mein kya seekhein?

Suggested order
Objects
Spread & Rest
Map & Set
JSON
Iterators & Generators

Final quick revision

PointDetail
MapKoi bhi type ki key, get / set / has
SetSirf unique values, add / has
Unique array[...new Set(arr)]
OrderDono insertion order rakhte hain
JSONPehle object ya array mein convert
GroupingMap.groupBy