knpuniversity/javascript

The article about Map should mention the entries function

Closed this issue · 1 comments

stof commented

When you need both keys and values during the iteration over a Map, there is an easy way to do it with for of:

for (let [key, value] of myMap.entries()) {
// do something
}

This is much better than using for in, which is looping over iterable properties of the Map object (which won't do what you expect here).

Btw, Array has this method too in ES2015, in case you need to access both the value and the integer index (requires a polyfill for IE, of course).

Array, Map and Set are big friends in ES2015.

This is awesome! I've just added a note at sha: f5a038e

We'll also add a note to the video - fortunately, it hasn't been fully edited yet :).