Javascript Map on Node, with EcmaScript

124 views Asked by At

I'm using Map in NodeJS 0.10.36 by enabling harmony flag. I'm able to create a map, set and get data, but other methods like size, keys(), entries(), forEach yield undefined results.

var k = new Map();
k.set('a', 1);
k.set('b', 2);

console.log('Printing out b', k.get('b')); //prints 2

var length = k.size;
console.log('Size of my map', length); // prints undefined

for(var key in k.keys()) {
    console.log('keys value',key);
} // undefined

Is there something that I could do to make it work?

1

There are 1 answers

0
Amadan On

You have an old Node version. My Node (0.12.4) prints 2 for k.size. k.keys() should be iterated with of, not with in:

for(var key of k.keys()) {
    console.log('keys value',key);
}

tl;dr: Upgrade Node.