-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48-map-data-strucutre.js
More file actions
80 lines (60 loc) · 2.1 KB
/
Copy path48-map-data-strucutre.js
File metadata and controls
80 lines (60 loc) · 2.1 KB
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
77
78
79
80
// map is an iterable
// It stores data in ordered fashion
// It stores key value pairs like objects
// Duplicate keys are not allowed like objects
/* difference between map and objects :
// objects can have only string or symbol as keys
// In map you can use anything as keys like array number strings
*/
const person = new Map();
person.set("firstName", "Vaishnao");
person.set("age", 21);
person.set(1, "One"); // Now here 1 is a number key
//You can have anytype as key
// person.set([1, 2, 3], "oneTwoThree");
// person.set({ 1: "one" }, "Object");
// console.log(person);
// console.log(person["firstName"]); // It will give undefined as output because we cannot use this method to access elements in map.
// // We use get method to do so
// console.log(person.get("firstName"));
// console.log(person.get("age"));
// console.log(person.get(1));
// // console.log(person.keys()); // to print all the keys
// // It provides a mapIterator so we can iterate through keys and output one by one
// for (let key of person.keys()) {
// console.log(key, typeof key);
// }
/* As maps are iterable we can use for of loop here , unlike objects */
// for (let key of person) {
// console.log(key);
// // It will give output as an array of key value pairs.
// // Ex - ['firstName', 'Vaishnao']
// // ['age', 21]
// // [1, 'One']
// }
// So we can use array destructuring to print key value seperately
// for (let [key, value] of person) {
// console.log(key, value);
// }
// Directly assigning values to map
// const user = new Map([
// ["firstName", "Vaishnao"],
// ["age", 21],
// ]);
// console.log(user);
/* We have objects and want to store their extra information somewhere using map */
const person1 = {
id: 1,
firstName: "Vaishnao",
};
const person2 = {
id: 2,
firstName: "Vaishnavi",
};
const extraInfo = new Map();
extraInfo.set(person1, { age: 21, gender: "Male" });
extraInfo.set(person2, { age: 22, gender: "Female" });
// console.log(extraInfo);
// We can access the following properties from maps as shown
console.log(extraInfo.get(person1).age);
console.log(extraInfo.get(person2).age);