Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,510 --> 00:00:05,500
So we learned about the forEach method on arrays.
2
00:00:05,500 --> 00:00:10,500
However, forEach is also available on maps and sets.
3
00:00:10,960 --> 00:00:13,510
And so let's take a small detour now
4
00:00:13,510 --> 00:00:17,793
and see how forEach works with maps and with sets.
5
00:00:19,450 --> 00:00:21,930
And we're gonna start with maps,
6
00:00:21,930 --> 00:00:25,980
and for that let's cut this currencies map
7
00:00:25,980 --> 00:00:27,333
that we have up here.
8
00:00:30,610 --> 00:00:32,020
All right.
9
00:00:32,020 --> 00:00:36,090
And remember that in this array of arrays,
10
00:00:36,090 --> 00:00:39,870
each of these array elements, so this inner array,
11
00:00:39,870 --> 00:00:42,110
will be one entry of the map,
12
00:00:42,110 --> 00:00:46,330
where this here is the key, and this is the value.
13
00:00:46,330 --> 00:00:47,600
All right.
14
00:00:47,600 --> 00:00:52,600
And so we can call forEach also on a map.
15
00:00:54,460 --> 00:00:59,460
So forEach, and then again, the callback function,
16
00:00:59,640 --> 00:01:03,700
and now this callback function also has three parameters.
17
00:01:03,700 --> 00:01:05,900
So when the forEach method calls it,
18
00:01:05,900 --> 00:01:09,220
it will call this function with three arguments.
19
00:01:09,220 --> 00:01:12,193
So the first one will be the current value,
20
00:01:13,220 --> 00:01:16,010
so the current value in the current iteration,
21
00:01:16,010 --> 00:01:19,500
the second one is the key, and the third one
22
00:01:19,500 --> 00:01:23,820
is the entire map that is being looped over.
23
00:01:23,820 --> 00:01:26,600
And so you see, this is similar to the array,
24
00:01:26,600 --> 00:01:28,530
where in the array,
25
00:01:28,530 --> 00:01:31,990
the first parameter is the current element of the array,
26
00:01:31,990 --> 00:01:33,720
the second one is the index
27
00:01:33,720 --> 00:01:36,350
and the third is the entire array.
28
00:01:36,350 --> 00:01:40,040
And so there is a nice correspondence between these,
29
00:01:40,040 --> 00:01:42,603
and so this is quite easy to memorize here.
30
00:01:43,540 --> 00:01:47,040
And so let's just log a very quick string here.
31
00:01:49,720 --> 00:01:51,780
So this is not really that important,
32
00:01:51,780 --> 00:01:55,810
but I wanted to show you that forEach also exists
33
00:01:55,810 --> 00:01:58,003
on these two new data structures.
34
00:01:59,200 --> 00:02:02,420
Now, okay, and indeed it worked.
35
00:02:02,420 --> 00:02:05,640
So we got the key of each of the map entries
36
00:02:05,640 --> 00:02:08,570
and then also the value.
37
00:02:08,570 --> 00:02:12,703
Alright, and so now let's try the same with a set.
38
00:02:14,070 --> 00:02:18,933
So this is with a map and this is now with a set.
39
00:02:19,780 --> 00:02:23,183
So let's create currenciesUnique.
40
00:02:25,550 --> 00:02:29,500
Let's say, so new set.
41
00:02:29,500 --> 00:02:33,150
And remember that in here, we need to pass an Iterable.
42
00:02:33,150 --> 00:02:36,310
And so I will use an array here.
43
00:02:36,310 --> 00:02:40,283
So let's just create a couple of strings here,
44
00:02:42,030 --> 00:02:46,173
another US dollar, and then some Euros.
45
00:02:49,330 --> 00:02:50,523
Let's just take a look,
46
00:02:52,920 --> 00:02:56,033
and indeed it only has the unique values.
47
00:02:56,900 --> 00:02:58,820
So just as expected,
48
00:02:58,820 --> 00:03:02,903
and so let's now call forEach on this set as well.
49
00:03:04,270 --> 00:03:05,870
So the currenciesUnique.forEach.
50
00:03:08,960 --> 00:03:12,120
And now once again, the callback function
51
00:03:12,120 --> 00:03:16,600
has the parameters of value, key and map,
52
00:03:18,570 --> 00:03:22,283
and we will see in a second, if that actually makes sense.
53
00:03:23,510 --> 00:03:25,183
So let's look to the console.
54
00:03:26,430 --> 00:03:28,030
Actually the same thing as here.
55
00:03:30,960 --> 00:03:33,310
So let's see what's gonna happen.
56
00:03:33,310 --> 00:03:38,013
So remember we are logging the key and the value, okay?
57
00:03:38,960 --> 00:03:43,960
But now we get USD, USD, and here GBP, GBP.
58
00:03:45,670 --> 00:03:48,620
And so what this means is that the key here
59
00:03:48,620 --> 00:03:52,060
is exactly the same as the value.
60
00:03:52,060 --> 00:03:54,240
So why is that?
61
00:03:54,240 --> 00:03:57,880
Well, a set doesn't have keys, right?
62
00:03:57,880 --> 00:04:00,040
And it doesn't have indexes either.
63
00:04:00,040 --> 00:04:04,410
And so there is no value that would make sense for the key.
64
00:04:04,410 --> 00:04:07,460
All right, so essentially this key here
65
00:04:07,460 --> 00:04:09,050
makes no sense at all.
66
00:04:09,050 --> 00:04:11,450
It wouldn't even have to be there.
67
00:04:11,450 --> 00:04:16,450
And so the people who designed this forEach method for sets,
68
00:04:16,520 --> 00:04:20,760
they could have simply omitted the second argument, right?
69
00:04:20,760 --> 00:04:22,860
Well, if they did that,
70
00:04:22,860 --> 00:04:26,600
then this forEach would have been different from the others.
71
00:04:26,600 --> 00:04:30,400
And so that would then create confusion in developers,
72
00:04:30,400 --> 00:04:33,980
and therefore it was decided to keep the same signature.
73
00:04:33,980 --> 00:04:38,080
So basically to keep the same three parameters
74
00:04:38,080 --> 00:04:40,330
in this callback function and simply
75
00:04:40,330 --> 00:04:44,210
to set the second one to the first one.
76
00:04:44,210 --> 00:04:47,523
So we can just write value here as well,
77
00:04:49,470 --> 00:04:51,433
just to avoid that confusion.
78
00:04:53,030 --> 00:04:53,863
All right.
79
00:04:55,250 --> 00:04:56,550
Now, and of course,
80
00:04:56,550 --> 00:05:00,010
we cannot have the duplicate parameter name.
81
00:05:00,010 --> 00:05:02,460
And so we can just use an underscore,
82
00:05:02,460 --> 00:05:05,750
which in JavaScript means a throwaway variable.
83
00:05:05,750 --> 00:05:09,550
So that means a variable that is completely unnecessary.
84
00:05:09,550 --> 00:05:12,460
So it's just a convention which we will see again
85
00:05:12,460 --> 00:05:13,573
a little bit later.
86
00:05:15,610 --> 00:05:17,200
Okay, and so now it should work
87
00:05:18,480 --> 00:05:20,640
and yeah, that's all you need to know
88
00:05:20,640 --> 00:05:24,360
about the forEach method for maps and sets.
89
00:05:24,360 --> 00:05:27,810
So it's very straightforward if you already understood
90
00:05:27,810 --> 00:05:29,493
how it works for arrays.
6677
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.