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