-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
54 lines (36 loc) · 977 Bytes
/
map.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
// A Map in JavaScript is a collection of key-value pairs, similar to an object, but with a few key differences
// map is iterable but object is not iterable
const map = new Map();
// add
map.set("name", "kalidas");
map.set("age", 25);
map.set(true, null);
console.log(map)
// get
console.log(map.get("name"))
map.forEach((value, key) => { // 1st value then key
console.log(value, key)
})
// update
map.set("name", "dhanush")
// delete
map.delete(true);
// check
console.log(map.has("name"))
console.log(map.has("true"))
console.log(map.size)
console.log(map);
console.log(new Map([["name", "kalidas"], ["age", 25]]))
// weakmap
const map2 = new Map(); // garbage issue
let obj = { name: "kalidas" }
map2.set("data", obj);
console.log(map2);
obj = null;
console.log(map2);
const weakmap = new WeakMap(); // WeakMap key is object type
let key = { name: "kalidas" }
weakmap.set(key, "data");
console.log(weakmap.get(key));
key = null;
console.log(weakmap);