Looping through Map collections

Let's have some Map collection:

let map = new Map; let arr1 = [1, 2]; let arr2 = [3, 4]; map.set(arr1, 'data1'); map.set(arr2, 'data2');

This collection can be iterated using the for-of loop:

for (let elem of map) { }

In this case, arrays of key-value pairs will fall into the elem. The first element will be the key and the second element will be the value:

for (let elem of map) { console.log(elem); // first [[1, 2], 'data1'], then [[3, 4], 'data2'] }

You can separate keys and values using destructuring:

for (let [key, elem] of map) { console.log(key); console.log(elem); }
enru