我正在使用Angular 4创建前端,并且遇到了一个奇怪的错误。每当我执行下面的代码:无法遍历角4中的地图
private createProductTree(productData: Map<number, any>): Map<number, Node> {
const productTree = new Map<number, Node>();
// Iterate over menu-item objects.
for (let key of productData.keys()) {
const product = productData.get(key);
if (product.type === 'menu-item') {
const node = new Node(product);
productTree.set(key, node);
}
}
return productTree;
}
我得到以下错误:在for (... of ...)
线Type 'IterableIterator<number>' is not an array type or a string type.
。
我不知道我在做什么错。 for
代码与我看到的代码here相同。
根据[讨论](https://github.com/ Microsoft/TypeScript/issues/6842),它可能与您定位的Javascript版本有关。它应该适用于ES6。 – ConnorsFan
@ConnorsFan,即使在es6中,这也不是迭代Map的正确方法,正如我在答案中提到的。如果使用'keys()',则不需要使用'for..of'。 'forEach'将是更好的选择 –