简体   繁体   English

为什么我不能迭代我的ES6地图的属性?

[英]Why can't I iterate the properties of my ES6 Map?

In the Node.js REPL: 在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 . 如您所见, foobar键是在map中明确定义的,但是当我尝试使用Map.prototype.forEach对其进行迭代时,什么也没发生-但根据MDN,它应该是 Note also that Map.prototype.forEach is defined , so it's not just that this method hasn't been implemented yet. 还要注意, 已经定义Map.prototype.forEach ,所以这不仅是尚未实现此方法。 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 . 我也尝试过使用for ... of ...循环,结果相同-我提供的每次迭代运行的代码实际上都不会运行,即使它应该运行。

I'm on Node.js v4.4.4. 我正在使用Node.js v4.4.4。 I searched the web for "javascript map isn't iterable node" and the like, with no luck. 我在网上搜索了“ javascript映射不是可迭代的节点”之类的内容,但是没有运气。

What's going on here? 这里发生了什么?

Not 100% positive, but I think you need to use map.set with Maps . 不是100%肯定,但我认为您需要将map.setMaps map.set使用。

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.prototype.forEach正在寻找可迭代的属性,所以您需要使用set()函数来设置这些可迭代的属性。

Map is an abstract data type . Map是一种抽象的数据类型 Applying the Map constructor with new creates a normal object of course. 当然,将Map构造函数与new会创建一个普通对象。 But the Map itself is abstracted behind its API ( Map.prototype.get/set/has/clear etc.): 但是Map本身是在其API之后抽象的( Map.prototype.get/set/has/clear等):

 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 . 请注意,这实际上是新Map类型的好处:它将数据与程序级别分开。

We can use for(){...} operator to iterate ES6 Map 我们可以使用for(){...}运算符来迭代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(). 如果我们想使用.map()迭代器,则可以对Maps使用一个简单的技巧,因为对于Maps,没有像map()这样的操作。 The approach from Dr. Axel Rauschmayer is: * Convert the map into an array of [key,value] pairs. Axel Rauschmayer博士的方法是:*将地图转换成[键,值]对数组。 * 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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM