-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16MapandSet.html
76 lines (62 loc) · 2.64 KB
/
Day16MapandSet.html
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html>
<head>
<title>
Day16
</title>
</head>
<body>
<script>
// Map
let map=new Map();
map.set('day',1);
map.set(1,'day');
console.log(map.get('day'));// 1
console.log(map.size);
let hello={name:'ponmani'}
map.set(hello,'hey');
console.log(map.get(hello));// even objects can be used as a key
//getting first element form map
console.log('First element>>',Array.from(map)[0])// converts map to array and getting first element
console.log('last element',Array.from(map)[map.size-1]);// gets last element form map
console.log('first elemetn nextt',map.entries().next().value);//total first map
console.log('first key nextt',map.keys().next().value);// first map key
console.log('first value nextt',map.values().next().value);//first map value
for(let m of map.keys()){
console.log('keys',m);//keys will print
}
for(let m1 of map.values()){
console.log('values',m1);//values will print
}
for(let m2 of map){
console.log('entries',m2);// key and values will print
}
//Object.entries() ---- acpt object and return array
let anyobj={
fname:'hello',
lname:'ponmani'
}
let maped=new Map(Object.entries(anyobj));
console.log('mapped',maped);//will array containing anyobject
//Object.fromEntries()------ opp to entries [acpt map/array and return object]
let map1=new Map([['apple',2],['orange',3]]);
let map2=new Map();
map2.set('apple',1);
map2.set('orange',3);
console.log(map1);//this
console.log(map2);// and are same
let helobjec=Object.fromEntries(map1);
console.log(helobjec);//{apple:2,orange:3}------ map converted to obj here
//Set------ wont accpt duplicate values
let set1=new Set();
set1.add('apple',1,'hello');//eventhough if we add any no of values , it will only first value
console.log(set1);//apple
set1.add('hello');
set1.forEach((value,valueagain,set)=>{
console.log('for1',value);//apple
console.log('for2',valueagain);//apple --- just to have compatablity with map this happened
console.log('for3',set);// total set
});
</script>
</body>
</html>