So we learned about the forEach method on arrays.
However, forEach is also available on maps and sets.
And so let's take a small detour now
and see how forEach works with maps and with sets.
And we're gonna start with maps,
and for that let's cut this currencies map
that we have up here.
All right.
And remember that in this array of arrays,
each of these array elements, so this inner array,
will be one entry of the map,
where this here is the key, and this is the value.
All right.
And so we can call forEach also on a map.
So forEach, and then again, the callback function,
and now this callback function also has three parameters.
So when the forEach method calls it,
it will call this function with three arguments.
So the first one will be the current value,
so the current value in the current iteration,
the second one is the key, and the third one
is the entire map that is being looped over.
And so you see, this is similar to the array,
where in the array,
the first parameter is the current element of the array,
the second one is the index
and the third is the entire array.
And so there is a nice correspondence between these,
and so this is quite easy to memorize here.
And so let's just log a very quick string here.
So this is not really that important,
but I wanted to show you that forEach also exists
on these two new data structures.
Now, okay, and indeed it worked.
So we got the key of each of the map entries
and then also the value.
Alright, and so now let's try the same with a set.
So this is with a map and this is now with a set.
So let's create currenciesUnique.
Let's say, so new set.
And remember that in here, we need to pass an Iterable.
And so I will use an array here.
So let's just create a couple of strings here,
another US dollar, and then some Euros.
Let's just take a look,
and indeed it only has the unique values.
So just as expected,
and so let's now call forEach on this set as well.
So the currenciesUnique.forEach.
And now once again, the callback function
has the parameters of value, key and map,
and we will see in a second, if that actually makes sense.
So let's look to the console.
Actually the same thing as here.
So let's see what's gonna happen.
So remember we are logging the key and the value, okay?
But now we get USD, USD, and here GBP, GBP.
And so what this means is that the key here
is exactly the same as the value.
So why is that?
Well, a set doesn't have keys, right?
And it doesn't have indexes either.
And so there is no value that would make sense for the key.
All right, so essentially this key here
makes no sense at all.
It wouldn't even have to be there.
And so the people who designed this forEach method for sets,
they could have simply omitted the second argument, right?
Well, if they did that,
then this forEach would have been different from the others.
And so that would then create confusion in developers,
and therefore it was decided to keep the same signature.
So basically to keep the same three parameters
in this callback function and simply
to set the second one to the first one.
So we can just write value here as well,
just to avoid that confusion.
All right.
Now, and of course,
we cannot have the duplicate parameter name.
And so we can just use an underscore,
which in JavaScript means a throwaway variable.
So that means a variable that is completely unnecessary.
So it's just a convention which we will see again
a little bit later.
Okay, and so now it should work
and yeah, that's all you need to know
about the forEach method for maps and sets.
So it's very straightforward if you already understood
how it works for arrays.