Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
So we learned about the forEach method on arrays. 2
2
However, forEach is also available on maps and sets. 3
3
And so let's take a small detour now 4
4
and see how forEach works with maps and with sets. 5
5
And we're gonna start with maps, 6
6
and for that let's cut this currencies map 7
7
that we have up here. 8
8
All right. 9
9
And remember that in this array of arrays, 10
10
each of these array elements, so this inner array, 11
11
will be one entry of the map, 12
12
where this here is the key, and this is the value. 13
13
All right. 14
14
And so we can call forEach also on a map. 15
15
So forEach, and then again, the callback function, 16
16
and now this callback function also has three parameters. 17
17
So when the forEach method calls it, 18
18
it will call this function with three arguments. 19
19
So the first one will be the current value, 20
20
so the current value in the current iteration, 21
21
the second one is the key, and the third one 22
22
is the entire map that is being looped over. 23
23
And so you see, this is similar to the array, 24
24
where in the array, 25
25
the first parameter is the current element of the array, 26
26
the second one is the index 27
27
and the third is the entire array. 28
28
And so there is a nice correspondence between these, 29
29
and so this is quite easy to memorize here. 30
30
And so let's just log a very quick string here. 31
31
So this is not really that important, 32
32
but I wanted to show you that forEach also exists 33
33
on these two new data structures. 34
34
Now, okay, and indeed it worked. 35
35
So we got the key of each of the map entries 36
36
and then also the value. 37
37
Alright, and so now let's try the same with a set. 38
38
So this is with a map and this is now with a set. 39
39
So let's create currenciesUnique. 40
40
Let's say, so new set. 41
41
And remember that in here, we need to pass an Iterable. 42
42
And so I will use an array here. 43
43
So let's just create a couple of strings here, 44
44
another US dollar, and then some Euros. 45
45
Let's just take a look, 46
46
and indeed it only has the unique values. 47
47
So just as expected, 48
48
and so let's now call forEach on this set as well. 49
49
So the currenciesUnique.forEach. 50
50
And now once again, the callback function 51
51
has the parameters of value, key and map, 52
52
and we will see in a second, if that actually makes sense. 53
53
So let's look to the console. 54
54
Actually the same thing as here. 55
55
So let's see what's gonna happen. 56
56
So remember we are logging the key and the value, okay? 57
57
But now we get USD, USD, and here GBP, GBP. 58
58
And so what this means is that the key here 59
59
is exactly the same as the value. 60
60
So why is that? 61
61
Well, a set doesn't have keys, right? 62
62
And it doesn't have indexes either. 63
63
And so there is no value that would make sense for the key. 64
64
All right, so essentially this key here 65
65
makes no sense at all. 66
66
It wouldn't even have to be there. 67
67
And so the people who designed this forEach method for sets, 68
68
they could have simply omitted the second argument, right? 69
69
Well, if they did that, 70
70
then this forEach would have been different from the others. 71
71
And so that would then create confusion in developers, 72
72
and therefore it was decided to keep the same signature. 73
73
So basically to keep the same three parameters 74
74
in this callback function and simply 75
75
to set the second one to the first one. 76
76
So we can just write value here as well, 77
77
just to avoid that confusion. 78
78
All right. 79
79
Now, and of course, 80
80
we cannot have the duplicate parameter name. 81
81
And so we can just use an underscore, 82
82
which in JavaScript means a throwaway variable. 83
83
So that means a variable that is completely unnecessary. 84
84
So it's just a convention which we will see again 85
85
a little bit later. 86
86
Okay, and so now it should work 87
87
and yeah, that's all you need to know 88
88
about the forEach method for maps and sets. 89
89
So it's very straightforward if you already understood 90
90
how it works for arrays.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.