最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why can't I iterate the properties of my ES6 Map? - Stack Overflow

programmeradmin1浏览0评论

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 using new Map([['foo', 'bar'], ['bar', 'baz']]) – Bergi Commented Sep 22, 2016 at 6:13
Add a ment  | 

5 Answers 5

Reset to default 7

Not 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 }));

发布评论

评论列表(0)

  1. 暂无评论