我试图迭代一个typescript映射,但我一直得到错误,我找不到任何解决方案,这样一个微不足道的问题。

我的代码是:

myMap : Map<string, boolean>;
for(let key of myMap.keys()) {
   console.log(key);
}

然后我得到Error:

类型'IterableIteratorShim<[string, boolean]>'不是数组类型或字符串类型。

全栈跟踪:

 Error: Typescript found the following errors:
  /home/project/tmp/broccoli_type_script_compiler-input_base_path-q4GtzHgb.tmp/0/src/app/project/project-data.service.ts (21, 20): Type 'IterableIteratorShim<[string, boolean]>' is not an array type or a string type.
    at BroccoliTypeScriptCompiler._doIncrementalBuild (/home/project/node_modules/angular-cli/lib/broccoli/broccoli-typescript.js:115:19)
    at BroccoliTypeScriptCompiler.build (/home/project/node_modules/angular-cli/lib/broccoli/broccoli-typescript.js:43:10)
    at /home/project/node_modules/broccoli-caching-writer/index.js:152:21
    at lib$rsvp$$internal$$tryCatch (/home/project/node_modules/rsvp/dist/rsvp.js:1036:16)
    at lib$rsvp$$internal$$invokeCallback (/home/project/node_modules/rsvp/dist/rsvp.js:1048:17)
    at lib$rsvp$$internal$$publish (/home/project/node_modules/rsvp/dist/rsvp.js:1019:11)
    at lib$rsvp$asap$$flush (/home/project/node_modules/rsvp/dist/rsvp.js:1198:9)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

我使用angular-cli beta5和typescript 1.8.10,我的目标是es5。 有人遇到过这个问题吗?


当前回答

es6

for (let [key, value] of map) {
    console.log(key, value);
}

es5

for (let entry of Array.from(map.entries())) {
    let key = entry[0];
    let value = entry[1];
}

其他回答

使用Array.from、Array.prototype.forEach()和箭头函数:

遍历键:

Array.from(myMap.keys()).forEach(key => console.log(key));

遍历值:

Array.from(myMap.values()).forEach(value => console.log(value));

遍历条目:

Array.from(myMap.entries()).forEach(entry => console.log('Key: ' + entry[0] + ' Value: ' + entry[1]));

如果你真的不喜欢嵌套函数,你也可以遍历键:

myMap : Map<string, boolean>;
for(let key of myMap) {
   if (myMap.hasOwnProperty(key)) {
       console.log(JSON.stringify({key: key, value: myMap[key]}));
   }
}

注意,您必须使用hasOwnProperty过滤掉非键迭代,如果您不这样做,就会得到警告或错误。

在Typescript 3.5和Angular 8 LTS中,需要按如下方式强制转换类型:

for (let [k, v] of Object.entries(someMap)) {
    console.log(k, v)
}
let map = new Map();

map.set('a', 2);
map.set('b', 4);
map.set('c', 6);
map.set('d', 7);

for(let key of map) 
{ 
    console.log(key[0], "----" key[1]) 
}

输出-

a ---- 2
b ---- 4
c ---- 6
d ---- 7

es6

for (let [key, value] of map) {
    console.log(key, value);
}

es5

for (let entry of Array.from(map.entries())) {
    let key = entry[0];
    let value = entry[1];
}