In fact, each iterable object contains not
one iterator, but three: values
,
keys
, and entries
. Each
object type has its own iterator by
default. For arrays, this is values
,
and, for example, for a Map
collection, this is entries
.
With this knowledge, we can now apply
this principle to any collection. For
example, let's say we have a Map
collection:
let map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
The default iterator for this
collection is entries
:
for (let elem of map) {
console.log(elem); // ['a', 1], ['b', 2], ['c', 3]
}
But we can easily get the keys of our collection:
for (let elem of map.keys()) {
console.log(elem); // 'a', 'b', 'c'
}
We can also get values:
for (let elem of map.values()) {
console.log(elem); // 1, 2, 3
}
What is the default iterator for a Set collection?