Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
161
rated 0 times [  164] [ 3]  / answers: 1 / hits: 94199  / 7 Years ago, wed, may 10, 2017, 12:00:00

Say we have a Map: let m = new Map();, using m.values() returns a map iterator.



But I can't use forEach() or map() on that iterator and implementing a while loop on that iterator seems like an anti-pattern since ES6 offer functions like map().



So is there a way to use map() on an iterator?


More From » dictionary

 Answers
29

The simplest and least performant way to do this is:



Array.from(m).map(([key,value]) => /* whatever */)


Better yet



Array.from(m, ([key, value]) => /* whatever */))


Array.from takes any iterable or array-like thing and converts it into an array! As Daniel points out in the comments, we can add a mapping function to the conversion to remove an iteration and subsequently an intermediate array.



Using Array.from will move your performance from O(1) to O(n) as @hraban points out in the comments. Since m is a Map, and they can't be infinite, we don't have to worry about an infinite sequence. For most instances, this will suffice.



There are a couple of other ways to loop through a map.



Using forEach



m.forEach((value,key) => /* stuff */ )


Using for..of



var myMap = new Map();
myMap.set(0, 'zero');
myMap.set(1, 'one');
for (var [key, value] of myMap) {
console.log(key + ' = ' + value);
}
// 0 = zero
// 1 = one

[#57837] Sunday, May 7, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
theo

Total Points: 680
Total Questions: 108
Total Answers: 81

Location: Laos
Member since Fri, Sep 11, 2020
4 Years ago
;