I was reading this example of using a for.. of
loop on a Map, and I was a bit confused with this syntax:
var myMap = new Map();
myMap.set(0, zero);
myMap.set(1, one);
for (var [key, value] of myMap) {
console.log(key + = + value);
}
Specifically, I don't understand the array destructuring that is happening. I understand that you can use array destructuring to do something like let [one, two] = [1, 2];
, but what is happening in this example? myMap
isn't an array so why is this getting the correct values?
Another question I have is why is the order key, value
in the destructuring, but when you do a forEach()
the order is value, key
, like here:
myMap.forEach((value, key) => {
console.log(key + = + value);
});