In the Node.js REPL:
> var map = new Map();
undefined
> map['foo'] = 'bar';
'bar'
> map['bar'] = 'baz';
'baz'
> map
Map { foo: 'bar', bar: 'baz' }
> map.forEach(console.log);
undefined
As you can see, the foo
and bar
keys are clearly defined within map
, but when I try to iterate over them with Map.prototype.forEach
, nothing happens - but according to MDN, it should. Note also that Map.prototype.forEach
is defined, so it's not just that this method hasn't been implemented yet. I've also tried using a for ... of ...
loop, with the same result - the code I provide to be run for each iteration doesn't actually run, even though it should.
I'm on Node.js v4.4.4. I searched the web for "javascript map isn't iterable node" and the like, with no luck.
What's going on here?
In the Node.js REPL:
> var map = new Map();
undefined
> map['foo'] = 'bar';
'bar'
> map['bar'] = 'baz';
'baz'
> map
Map { foo: 'bar', bar: 'baz' }
> map.forEach(console.log);
undefined
As you can see, the foo
and bar
keys are clearly defined within map
, but when I try to iterate over them with Map.prototype.forEach
, nothing happens - but according to MDN, it should. Note also that Map.prototype.forEach
is defined, so it's not just that this method hasn't been implemented yet. I've also tried using a for ... of ...
loop, with the same result - the code I provide to be run for each iteration doesn't actually run, even though it should.
I'm on Node.js v4.4.4. I searched the web for "javascript map isn't iterable node" and the like, with no luck.
What's going on here?
Share Improve this question edited Sep 22, 2016 at 6:15 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Sep 22, 2016 at 6:04 strugeestrugee 2,8004 gold badges22 silver badges34 bronze badges 1-
Map
objects don't have properties, they have entries. Create it usingnew Map([['foo', 'bar'], ['bar', 'baz']])
– Bergi Commented Sep 22, 2016 at 6:13
5 Answers
Reset to default 7Not 100% positive, but I think you need to use map.set
with Maps
.
var map = new Map();
map.set('foo', 'bar');
map.set('bar', 'baz');
map.forEach(console.log);
This will log out each of the items that you are looking for. Because the Map.prototype.forEach
is looking for iterable properties, you need to use the set()
function to set those iterable properties.
Map
is an abstract data type. Applying the Map
constructor with new
creates a normal object of course. But the Map
itself is abstracted behind its API (Map.prototype.get/set/has/clear
etc.):
const map = new Map([["key1", 1], ["key2", 2]]);
// you can mutate the Map object:
map["foo"] = "bar";
map.bar = "baz";
console.log( map );
// but you need to use the Map API to mutate the Map itself:
map.set("key3", 3);
map.forEach(console.log.bind(console));
Note that this is actually the benefit of the new Map type: It separates the data from the program level.
We can use for(){...} operator to iterate ES6 Map
let map0 = new Map([
["a", 1],
["b", 2],
["c", 3]
]);
let map1 = new Map();
for (let [key, value] of myMap) {
map1.set('_' + k, v * 2);
}
resulted in
{'_a' => 2, '_b' => 4, '_c' => 6}
If we want to use .map() iterator, we can use a simple trick for Maps, because there is no such operation for Maps as map(). The approach from Dr. Axel Rauschmayer is: * Convert the map into an array of [key,value] pairs. * Map or filter the array. * Convert the result back to a map.
Example:
let map0 = new Map([
["a", 1],
["b", 2],
["c", 3]
]);
const map1 = new Map(
[...map0]
.map(([k, v]) => ['_' + k, v * 2 ])
);
resulted in
{'_a' => 2, '_b' => 4, '_c' => 6}
For anybody who tries to convert a reactive Map<> property (in Vue.js) into an array, use Array.from(). Reactive properties won't map "as is":
const array = Array.from(reactiveMap.entries()).map(([key, value]) => ({ key, value }));