'map.values(); returns weird empty "Map Iterator { }" with no values [closed]

I am following a tutorial, in it a Map is created then map.Value() is used; this prints all the values of that map, the same as with map.Keys();

When I do it, however, I get this weird empty object Map Iterator { } with no values, and same without keys when I try map.keys():

Tutorial

Mine



Solution 1:[1]

Whatever the tutorial claims, the values method of a Map instance does not return an array, but an iterator.

You need to consume that iterator to get the actual values:

let mymap = new Map([[1, "a"], [2, "b"], [3, "c"]]);

console.log(mymap.values()); // Wrong: above values are not displayed

console.log(Array.from(mymap.values())); // Create array, or

console.log(...mymap.values()); // Spread as arguments

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 trincot