Skip to content

Commit af24b5c

Browse files
authored
Create 9Map&Set.js
1 parent 08c0880 commit af24b5c

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

AdvancedJS/9Map&Set.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//> Map
2+
// Map->Collection of unique Data , Map is object Constructor (update new value or override new player)
3+
let mymap=new Map([[-1,"RM-1"],[0,"RM0"]]);
4+
mymap.set(1,"RM1");//always in key value paits
5+
mymap.set(2,"RM2");
6+
mymap.set(3,"RM3");
7+
console.log(mymap);
8+
// Map(5) {-1 => 'RM-1', 0 => 'RM0', 1 => 'RM1', 2 => 'RM2', 3 => 'RM3'}
9+
// [[Prototype]]:Map
10+
11+
// We can use for loop and there are many other methods and key word also
12+
for(let [key,value] of mymap){
13+
console.log(key + " " + value);
14+
15+
}
16+
mymap.delete(2);//delete
17+
console.log(mymap);
18+
19+
mymap.clear();//clear
20+
console.log(mymap.get(-1));//return value of that index
21+
22+
23+
24+
// Weak MAP-> only store obj and cant be iterated
25+
26+
let wm=new WeakMap();
27+
let ob1={};
28+
let ob2={};
29+
30+
wm.set(ob1,"private");
31+
wm.set(ob2,"Private data");
32+
----------------------------------------Set------------------------------
33+
Set -> Collection of unique Data , Set is object Constructor
34+
35+
var arr=[1,4,7,2,5,4];
36+
let objSet=new Set(arr);
37+
console.log(objSet);
38+
39+
objSet.add(value);//add value
40+
objSet.delete(value);//delete value
41+
objSet.clear();//clear
42+
objSet.has();//check
43+
44+
45+
46+
47+
//Weak Set -> we can store only object here but we cant iterate it
48+
let ws=new WeakSet();
49+
let ob1={};
50+
let ob2={};
51+
ws.add(ob1);
52+
ws.add(ob2);
53+
54+
console.log(ws);
55+
56+
#Note-> All set Functions are available here but cant do iteration

0 commit comments

Comments
 (0)