-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathES6-Data-structures.js
More file actions
250 lines (207 loc) Β· 6.65 KB
/
Copy pathES6-Data-structures.js
File metadata and controls
250 lines (207 loc) Β· 6.65 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* π‘"JavaScript-with-JC"
π Map, Set, WeakMap, and WeakSet ( ES6 Data Structures ) */
// π‘Map Object
// π The Map object is a collection of key-value pairs having keys of any type ( functions, objects, primitive )
// π A Object's keys must be either a String or a Symbol whereas Map allows any type of key.
// π Map remembers the original insertion order of the keys.
// π A key in the Map may only occur once; it is unique in the Map's collection.
// π‘ Creation of Map :-
// π 1) Create Map Obj by Passing an array :-
const items = new Map([
["shirts", 150],
["pants", 200],
["shoes", 100],
]);
console.log(items); // Map(3)Β {'shirts' => 150, 'pants' => 200, 'shoes' => 100}
// π 2) Create Map Obj by new Map() and Map.set() :-
const players = new Map();
players.set("Virat", "Batsman");
players.set("Hardik", "All-Rounder");
players.set("Bhumrah", "Bowler");
console.log(players); // Map(3)Β {'Virat' => 'Batsman', 'Hardik' => 'All-Rounder', 'Bhumrah' => 'Bowler'}
// π check size of map using map.size
console.log(players.size); // 3
// π get value using map.get(key)
console.log(players.get("Hardik")); // All-Rounder
console.log(players.get("Rishab")); // undefined
// π check value exists using map.has(key)
console.log(players.has("Hardik")); // true
console.log(players.has("Rishab")); // false
// π‘ Map Iteration :-
// π 1) map.keys()
console.log(players.keys()); // [Map Iterator] { 'Virat', 'Hardik', 'Bhumrah' }
// π 2) map.values()
console.log(players.values()); // [Map Iterator] { 'Batsman', 'All-Rounder', 'Bowler' }
// π 3) map.entries()
console.log(players.entries());
/* π output
[Map Entries] {
[ 'Virat', 'Batsman' ],
[ 'Hardik', 'All-Rounder' ],
[ 'Bhumrah', 'Bowler' ]
}
*/
// π 4) Iterating Map with for...of
for (let [key, value] of players) {
console.log("name", key, "Role", value);
}
/* π output
name Virat Role Batsman
name Hardik Role All-Rounder
name Bhumrah Role Bowler
*/
// π 5) Iterating Map with forEach()
players.forEach((value, key) => {
console.log("name", key, "Role", value);
});
/* π output
name Virat Role Batsman
name Hardik Role All-Rounder
name Bhumrah Role Bowler
*/
// π delete value using map.delete(key)
console.log(players.delete("Hardik")); // true
console.log(players.delete("Rishab")); // false
console.log(players); // Map(3)Β { 'Virat' => 'Batsman', 'Bhumrah' => 'Bowler' }
// π clear all values using map.clear()
console.log(players.clear()); // undefined
console.log(players); // Map(0) {}
// π‘ Set Object
// π Set objects are collections of value of any type ( primitive or object).
// π A value in the Set may only occur once. It is unique in the Set's collection.
// π Set remembers the original insertion order of the values.
// π‘ Creation of Set
// π 1) Create Set Obj by Passing an array :-
const numbers = [1, 2, 3, 4, 5, 5];
const uniqueNumbers = new Set(numbers);
console.log(uniqueNumbers); // Set(5) { 1, 2, 3, 4, 5 }
const uniqueNumbersArray = [...new Set(numbers)];
console.log(uniqueNumbersArray); // [ 1, 2, 3, 4, 5 ]
// π 2) Create Set Obj by new Set() and Set.add() :-
const persons = new Set();
persons.add("Jayesh");
persons.add("Sam");
persons.add("John");
console.log(persons); // Set(3) { 'Jayesh', 'Sam', 'John' }
// π get size using set.size
console.log(persons.size); // 3
// π check value exists using set.has(value)
console.log(persons.has("Jayesh")); // true
console.log(persons.has("Jc")); // false
// π‘ Set Iteration
// π 1) set.keys()
console.log(persons.keys()); // [Set Iterator] { 'Jayesh', 'Sam', 'John' }
// π 2) set.values()
console.log(persons.values()); // [Set Iterator] { 'Jayesh', 'Sam', 'John' }
// π 3) set.entries()
console.log(persons.entries());
/* π output
[Set Entries] {
[ 'Jayesh', 'Jayesh' ],
[ 'Sam', 'Sam' ],
[ 'John', 'John' ]
}
*/
// π 4) Iterating Set with for...of
for (let value of persons) {
console.log(value);
}
/* π output
Jayesh
Sam
John
*/
// π 5) Iterating Set with forEach
persons.forEach((value) => {
console.log(value);
});
/* output
Jayesh
Sam
John
*/
// π delete value using set.delete(value)
console.log(persons.delete("Sam")); // true
console.log(persons.delete("Jc")); // false
console.log(persons); // Set(2) { 'Jayesh', 'John' }
// π clear all values using set.clear()
console.log(players.clear()); // undefined
console.log(players); // Map(0) {}
// π‘ WeakMap Object
// π A WeakMap is a collection of key/value pairs whose keys must be objects and values of any.
// π Primitive data types as keys are not allowed in WeakMap.
// π WeakMap does not remember the original insertion order of the keys.
// π WeakMap does not support iteration and methods keys(), values(), entries()
const students = new WeakMap();
let key1 = {
id: 1,
};
let student1 = {
name: "sam",
class: "6th",
};
let key2 = {
id: 2,
};
let student2 = {
name: "monu",
class: "7th",
};
students.set(key1, student1);
students.set(key2, student2);
console.log(students);
/* π output
[[Entries]]
0: {Object => Object}
key: {id: 2}
value: {name: 'monu', class: '7th'}
1: {Object => Object}
key: {id: 1}
value: {name: 'sam', class: '6th'}
*/
// π get value using WeakMap.get(key)
console.log(students.get(key2)); // { name: 'monu', class: '7th' }
// π check key exists using WeakMap.has(key)
console.log(students.has(key1)); // true
console.log(students.has(key2)); // true
// π delete key-value using WeakMap.delete(key)
console.log(students.delete(key1)); // true
console.log(students);
/* π output
[[Entries]]
0: {Object => Object}
key: {id: 2}
value: {name: 'monu', class: '7th'}
*/
// π‘ WeakSet Object
// π WeakSet objects are collections of objects ( primitive types not allowed ).
// π Each object in a WeakSet may occur only once, all objects in a WeakSet's collection are unique.
// π WeakSet does not remember the original insertion order of the keys.
// π WeakSet does not support size, iteration and methods keys(), values(), entries().
const employees = new WeakSet();
let employee1 = {
name: "John",
experience: "5 years",
};
let employee2 = {
name: "Steve",
experience: "8 years",
};
employees.add(employee1);
employees.add(employee2);
console.log(employees);
/* π output
[[Entries]]
0: value: {name: 'Steve', experience: '8 years'}
1: value: {name: 'John', experience: '5 years'}
*/
// π check key exists using WeakSet.has(key)
console.log(employees.has(employee1)); // true
console.log(employees.has(employee2)); // true
// π delete key-value using WeakSet.delete(key)
console.log(employees.delete(employee1)); // true
console.log(employees);
/* π output
[[Entries]]
0: value: {name: 'Steve', experience: '8 years'}
*/