Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,240 --> 00:00:03,260
So we learnt about sets.
2
00:00:03,260 --> 00:00:04,440
Now it's time to learn
3
00:00:04,440 --> 00:00:07,800
about the other new JavaScript data structure,
4
00:00:07,800 --> 00:00:09,473
and that is maps.
5
00:00:11,060 --> 00:00:12,800
And let me start by telling you,
6
00:00:12,800 --> 00:00:16,650
that maps are a lot more useful than sets.
7
00:00:16,650 --> 00:00:19,540
So what exactly is a map?
8
00:00:19,540 --> 00:00:22,210
Well, it's certainly not the same thing,
9
00:00:22,210 --> 00:00:26,360
that we use in the real life to find our ways around.
10
00:00:26,360 --> 00:00:30,640
No, instead in JavaScript, a map is a data structure,
11
00:00:30,640 --> 00:00:34,230
that we can use to map values to keys.
12
00:00:34,230 --> 00:00:35,970
So just like an object,
13
00:00:35,970 --> 00:00:39,670
data is stored in key value pairs in maps.
14
00:00:39,670 --> 00:00:42,950
Now the big difference between objects and maps,
15
00:00:42,950 --> 00:00:46,440
is that in maps, the keys can have any type.
16
00:00:46,440 --> 00:00:48,540
And this can be huge.
17
00:00:48,540 --> 00:00:52,240
So in objects, the keys are basically always strings.
18
00:00:52,240 --> 00:00:55,330
But in maps, we can have any type of key.
19
00:00:55,330 --> 00:00:59,520
It could even be objects or erase or other maps,
20
00:00:59,520 --> 00:01:03,253
so that can lead to some really great and advanced stuff.
21
00:01:04,810 --> 00:01:08,283
So let's create a restaurant map here,
22
00:01:10,130 --> 00:01:11,963
so I'm just calling it rest here.
23
00:01:13,470 --> 00:01:16,640
And so, we again use the constructor
24
00:01:16,640 --> 00:01:18,630
just like we used for the set,
25
00:01:18,630 --> 00:01:20,750
but this one called map.
26
00:01:20,750 --> 00:01:23,180
And easiest way to create a map,
27
00:01:23,180 --> 00:01:26,730
is to actually create an empty map just like this,
28
00:01:26,730 --> 00:01:29,140
without passing anything in.
29
00:01:29,140 --> 00:01:34,140
And then to fill up the map, we can use the set method.
30
00:01:36,600 --> 00:01:39,400
And then here we pass into arguments.
31
00:01:39,400 --> 00:01:43,860
The first is the key name, so let's call it name,
32
00:01:43,860 --> 00:01:46,023
and then the name of the restaurant itself.
33
00:01:48,000 --> 00:01:51,700
So Classico Italiano. Okay.
34
00:01:51,700 --> 00:01:55,230
So you'll see that this set method is pretty similar,
35
00:01:55,230 --> 00:01:58,450
to the add method that we had in sets.
36
00:01:58,450 --> 00:02:01,810
So both allow us to add a new element,
37
00:02:01,810 --> 00:02:03,440
to the data structure.
38
00:02:03,440 --> 00:02:08,440
And remember that we can use, any data type that we want.
39
00:02:08,490 --> 00:02:11,810
So let's say that the restaurant has two locations,
40
00:02:11,810 --> 00:02:15,070
so we can create a key with a number.
41
00:02:15,070 --> 00:02:17,850
It doesn't have to be a string.
42
00:02:17,850 --> 00:02:20,413
So let's say one is in 'Firenze, Italy.
43
00:02:23,030 --> 00:02:25,100
And the other one.
44
00:02:25,100 --> 00:02:30,027
So a number two is in Lisbon, Portugal,
45
00:02:31,620 --> 00:02:34,410
and actually calling the set method like this,
46
00:02:34,410 --> 00:02:36,303
does return the new set.
47
00:02:37,610 --> 00:02:41,283
So let me show that to you here, okay.
48
00:02:44,640 --> 00:02:46,900
So this is what the set looks like,
49
00:02:46,900 --> 00:02:49,533
after adding this key number two.
50
00:02:53,020 --> 00:02:56,120
So here we have the entries, the first is name,
51
00:02:56,120 --> 00:02:59,120
which has then mapped to classical Italiano
52
00:02:59,120 --> 00:03:01,810
and then the other two that we just added.
53
00:03:01,810 --> 00:03:06,580
Now the fact that the set method, returns the new set ,
54
00:03:06,580 --> 00:03:10,653
allows us to chain the set method like this.
55
00:03:13,690 --> 00:03:18,690
So let's set categories here, as an array
56
00:03:21,690 --> 00:03:23,743
and I will just grab that from up here.
57
00:03:28,460 --> 00:03:32,160
Okay. So once more, the value can be anything.
58
00:03:32,160 --> 00:03:35,673
And now I can just chain the next set on here.
59
00:03:37,578 --> 00:03:41,720
Okay. So I can now say open and set it to 11,
60
00:03:43,140 --> 00:03:44,480
and then again,
61
00:03:44,480 --> 00:03:49,480
I can chain the next one and say close at 23.
62
00:03:50,040 --> 00:03:51,240
So let's save it,
63
00:03:51,240 --> 00:03:53,763
and this will unformat this nicely.
64
00:03:54,670 --> 00:03:59,460
And so again calling the set method will return the new set.
65
00:03:59,460 --> 00:04:02,140
And so all of this here is now the new set.
66
00:04:02,140 --> 00:04:05,523
And so then on that set, we can call set again.
67
00:04:06,670 --> 00:04:08,090
All right.
68
00:04:08,090 --> 00:04:10,313
And we can continue this even further.
69
00:04:12,320 --> 00:04:16,600
So let's not actually even use Booleans as the key here.
70
00:04:16,600 --> 00:04:21,600
And we can say that, we want to map true to we are open.
71
00:04:23,870 --> 00:04:28,870
Okay. And then we can also set false to we are closed
72
00:04:34,630 --> 00:04:35,940
and we will see in a minute,
73
00:04:35,940 --> 00:04:40,940
how this can become useful, okay.
74
00:04:40,960 --> 00:04:43,360
Now, in order to read data from a map,
75
00:04:43,360 --> 00:04:45,670
we use the get method.
76
00:04:45,670 --> 00:04:49,563
And so that get method is available on all the maps.
77
00:04:50,680 --> 00:04:54,963
And so, all we need to do is to pass in the name of the key.
78
00:04:55,830 --> 00:04:59,590
So let's see, let's lo this to the console actually
79
00:05:01,200 --> 00:05:05,223
and let's just duplicate it here and also log true,
80
00:05:06,500 --> 00:05:10,010
and you'll see that the true key here,
81
00:05:10,010 --> 00:05:12,230
is then mapped to we are open
82
00:05:12,230 --> 00:05:15,590
and named to Classico Italiano, of course.
83
00:05:15,590 --> 00:05:17,700
And here, when we get the elements,
84
00:05:17,700 --> 00:05:20,810
of course the data type of the key matters.
85
00:05:20,810 --> 00:05:21,970
So if we try it
86
00:05:21,970 --> 00:05:25,023
through the string, then that would be undefined.
87
00:05:25,920 --> 00:05:29,990
And if we tried to retrieve one, as a string,
88
00:05:29,990 --> 00:05:31,710
that would also be undefined.
89
00:05:31,710 --> 00:05:36,050
Because here we have one that the number as the key.
90
00:05:36,050 --> 00:05:36,883
And so with this,
91
00:05:36,883 --> 00:05:41,883
we will then yet Firenze Italy, all right.
92
00:05:41,970 --> 00:05:44,670
Now, just for fun, let's use the fact
93
00:05:44,670 --> 00:05:46,860
that we can have Boolean keys.
94
00:05:46,860 --> 00:05:50,510
So this true and false here.
95
00:05:50,510 --> 00:05:54,790
Right. And we also have the open and the close time.
96
00:05:54,790 --> 00:05:57,370
So you'll see, they are all kind of related.
97
00:05:57,370 --> 00:06:00,103
And so let's create something fun with this.
98
00:06:01,620 --> 00:06:04,300
So let's say we have some current time,
99
00:06:04,300 --> 00:06:05,253
and we could actually get
100
00:06:05,253 --> 00:06:07,550
true current time from JavaScript,
101
00:06:07,550 --> 00:06:09,460
but we don't know how yet.
102
00:06:09,460 --> 00:06:12,050
So let's just say it's 21 hours.
103
00:06:12,050 --> 00:06:14,850
So that's a 9:00 PM.
104
00:06:14,850 --> 00:06:19,620
And then, we could do something really clever like this.
105
00:06:19,620 --> 00:06:22,310
So rest dot get,
106
00:06:22,310 --> 00:06:27,180
and so when we pass in true, then we will get, we are open.
107
00:06:27,180 --> 00:06:31,170
And when we pass in false, we will get, we are closed.
108
00:06:31,170 --> 00:06:33,830
And so in order to get the correct string here,
109
00:06:33,830 --> 00:06:35,410
according to the time,
110
00:06:35,410 --> 00:06:38,340
all we need to do is to compare the current time
111
00:06:38,340 --> 00:06:40,630
with these two open and close.
112
00:06:40,630 --> 00:06:43,093
Okay. So let's do that.
113
00:06:44,520 --> 00:06:49,510
We can say, or we can ask is the time,
114
00:06:49,510 --> 00:06:54,510
so the current time greater than rest dot get open,
115
00:06:57,700 --> 00:07:02,700
and at the same time, is it below rest dot get close?
116
00:07:07,310 --> 00:07:09,610
So basically here we are asking,
117
00:07:09,610 --> 00:07:13,360
if the current time is between 23 and 11.
118
00:07:13,360 --> 00:07:16,900
And so this here is a true false value,
119
00:07:16,900 --> 00:07:18,920
and this is a true false value.
120
00:07:18,920 --> 00:07:20,850
So Boolean, and so the results
121
00:07:20,850 --> 00:07:24,580
of this end operation, will be either true or false.
122
00:07:24,580 --> 00:07:29,580
And then true or false will map to one of these values.
123
00:07:30,660 --> 00:07:34,410
So that's really clever right? Let's log
124
00:07:34,410 --> 00:07:39,390
the results to the console and let's check it out.
125
00:07:39,390 --> 00:07:42,340
And so at 21 hours, we are open
126
00:07:43,260 --> 00:07:44,790
and that makes sense.
127
00:07:44,790 --> 00:07:46,400
It is between these two times
128
00:07:47,390 --> 00:07:52,050
but if it was like eight hours, then we are closed.
129
00:07:52,050 --> 00:07:57,050
Amazing. Now this is as I said, pretty clever,
130
00:07:57,700 --> 00:08:00,170
but it's not really so readable.
131
00:08:00,170 --> 00:08:02,900
So don't overuse this kind of pattern.
132
00:08:02,900 --> 00:08:06,210
Okay. This just really goes to show the power
133
00:08:06,210 --> 00:08:09,080
of having Booleans as map keys.
134
00:08:09,080 --> 00:08:11,560
But anyway let's now keep exploring,
135
00:08:11,560 --> 00:08:14,750
the methods that are available on maps.
136
00:08:14,750 --> 00:08:18,100
So we already have methods to set and to get.
137
00:08:18,100 --> 00:08:22,273
Now we can also check if a map contains a certain key.
138
00:08:23,810 --> 00:08:27,420
So let's log to the console the result of calling
139
00:08:27,420 --> 00:08:29,513
the has method.
140
00:08:30,540 --> 00:08:34,460
So categories and so this should be true
141
00:08:34,460 --> 00:08:36,670
and it is indeed true.
142
00:08:36,670 --> 00:08:40,260
Then we can also delete elements from the map,
143
00:08:40,260 --> 00:08:43,920
and again, that happens based on the key.
144
00:08:43,920 --> 00:08:46,650
So rest dot delete.
145
00:08:46,650 --> 00:08:50,970
And let's say that the second location should be close now.
146
00:08:50,970 --> 00:08:53,880
So this one here, the one in Lisbon.
147
00:08:53,880 --> 00:08:57,193
And so we take key and delete it from the map.
148
00:08:58,270 --> 00:09:02,543
And as a result, that will then be gone here.
149
00:09:03,650 --> 00:09:06,180
So you see, no longer there.
150
00:09:06,180 --> 00:09:08,563
Now comparing this to objects,
151
00:09:09,500 --> 00:09:13,180
we can actually also delete properties from objects,
152
00:09:13,180 --> 00:09:16,650
using something called a delete operator,
153
00:09:16,650 --> 00:09:18,690
but that's a really slow process.
154
00:09:18,690 --> 00:09:22,320
And usually it's not encouraged to do that.
155
00:09:22,320 --> 00:09:24,960
I hear about the has method,
156
00:09:24,960 --> 00:09:28,250
actually objects do also have a method,
157
00:09:28,250 --> 00:09:30,800
which is called has owned property.
158
00:09:30,800 --> 00:09:31,633
But we're gonna talk
159
00:09:31,633 --> 00:09:35,173
about that in the object oriented programming section.
160
00:09:37,110 --> 00:09:42,110
All right, next maps also have to size property.
161
00:09:43,420 --> 00:09:47,900
So rest dot size, of course,
162
00:09:47,900 --> 00:09:50,393
and that should be seven as we can see here.
163
00:09:51,720 --> 00:09:55,150
And yeah, so it has seven items.
164
00:09:55,150 --> 00:09:58,290
And then to finish, we can also clear
165
00:09:58,290 --> 00:10:02,723
so basically remove all the elements from the map.
166
00:10:04,540 --> 00:10:07,480
Let's just put that here.
167
00:10:07,480 --> 00:10:11,670
And so now it's gone and it has a size of zero.
168
00:10:11,670 --> 00:10:12,853
Let's put that back.
169
00:10:13,730 --> 00:10:15,120
So as you can see here,
170
00:10:15,120 --> 00:10:19,170
there is some overlap in the way that we work with maps
171
00:10:19,170 --> 00:10:22,350
and sets and that's because they were both introduced
172
00:10:22,350 --> 00:10:23,523
in year six.
173
00:10:24,580 --> 00:10:26,630
And now just to finish,
174
00:10:26,630 --> 00:10:30,280
let me show you that we can in fact, use arrays
175
00:10:30,280 --> 00:10:35,280
or objects as map keys, so let's see, or set something else.
176
00:10:38,220 --> 00:10:42,090
And so now as a key, I will use this array
177
00:10:43,350 --> 00:10:47,510
and I will set it to test.
178
00:10:47,510 --> 00:10:50,690
Okay. And let's just put that actually here.
179
00:10:50,690 --> 00:10:54,473
So it's before the log, so we can take a look at it.
180
00:10:55,740 --> 00:11:00,070
And so here indeed we see, this is now the key,
181
00:11:00,070 --> 00:11:03,610
this array, all right?
182
00:11:03,610 --> 00:11:05,660
But now to get the data based,
183
00:11:05,660 --> 00:11:10,450
on that array, let me see how we could do that.
184
00:11:10,450 --> 00:11:15,450
So rest dot get, and think about what we are doing here.
185
00:11:17,540 --> 00:11:19,190
Do you think that this will work?
186
00:11:20,580 --> 00:11:23,053
and actually we need to log it to console,
187
00:11:24,210 --> 00:11:25,970
but given what we learnt,
188
00:11:25,970 --> 00:11:29,270
in the previous section, about how JavaScript works
189
00:11:29,270 --> 00:11:34,070
behind the scenes, especially primitives versus objects,
190
00:11:34,070 --> 00:11:37,523
do you think that this will now retrieve test?
191
00:11:39,290 --> 00:11:43,120
Well, let's see and no, it did not.
192
00:11:43,120 --> 00:11:46,830
And the reason for that, is that these two arrays
193
00:11:46,830 --> 00:11:49,630
are actually not the same object.
194
00:11:49,630 --> 00:11:50,980
So this array
195
00:11:50,980 --> 00:11:54,330
and this one, even though we wrote them in the same way.
196
00:11:54,330 --> 00:11:56,750
And so they have the same elements,
197
00:11:56,750 --> 00:11:59,960
they are not the same object in the heap.
198
00:11:59,960 --> 00:12:04,960
Okay. And the key here is exactly this object,
199
00:12:05,500 --> 00:12:09,000
this object in memory and not this one.
200
00:12:09,000 --> 00:12:12,910
And so this cannot work, in order to make it work.
201
00:12:12,910 --> 00:12:15,033
We would have to do this,
202
00:12:16,120 --> 00:12:21,120
creating an array, one, two, and then we use that array.
203
00:12:23,450 --> 00:12:25,750
And then we also use the same array,
204
00:12:25,750 --> 00:12:28,980
to read the value out of the map.
205
00:12:28,980 --> 00:12:30,723
And so now it is gonna work.
206
00:12:31,760 --> 00:12:33,810
Okay, because now of course,
207
00:12:33,810 --> 00:12:37,550
these two refer to the same place in memory.
208
00:12:37,550 --> 00:12:38,810
Great. And so with this
209
00:12:38,810 --> 00:12:43,810
we proved that we can indeed, use objects as map keys.
210
00:12:44,740 --> 00:12:47,810
And this can be very useful with dumb elements.
211
00:12:47,810 --> 00:12:51,250
Which in fact are also nothing more,
212
00:12:51,250 --> 00:12:54,160
than just a special type of object.
213
00:12:54,160 --> 00:12:55,923
Let's do that here as well.
214
00:12:57,790 --> 00:13:01,740
Let's do rest dot set,
215
00:13:01,740 --> 00:13:06,740
and now as a key, we will use document dot query selector,
216
00:13:07,940 --> 00:13:10,450
and then we're gonna select this H one elements,
217
00:13:10,450 --> 00:13:11,920
that we have here.
218
00:13:11,920 --> 00:13:13,463
So this is just an H one.
219
00:13:14,390 --> 00:13:16,070
So nothing fancy,
220
00:13:16,070 --> 00:13:19,630
and so the result of this will be an object.
221
00:13:19,630 --> 00:13:22,513
And so let's say that this is the heading.
222
00:13:24,330 --> 00:13:28,590
And now as we check it out here, you will see that is here,
223
00:13:28,590 --> 00:13:31,340
indeed. So as I hover it,
224
00:13:31,340 --> 00:13:34,580
you can even see it here visible on the webpage.
225
00:13:34,580 --> 00:13:36,790
You see that the highlight up there,
226
00:13:36,790 --> 00:13:41,270
that exactly is the key of this map entry.
227
00:13:41,270 --> 00:13:43,790
sounds crazy, but it is possible
228
00:13:43,790 --> 00:13:47,700
and it can enable some advanced functionality.
229
00:13:47,700 --> 00:13:50,620
Alright. So we learned here how to create maps
230
00:13:50,620 --> 00:13:52,570
and how to work with them.
231
00:13:52,570 --> 00:13:55,290
So this should be enough for one lecture.
232
00:13:55,290 --> 00:13:57,850
And so let's wrap up this one now
233
00:13:57,850 --> 00:14:01,143
and continue learning about maps in the next one.
17270
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.