CNode

js map 循环

分享
Ggocpplua发布于5 年前最后回复5 年前1 回复3555 浏览0 收藏
// --target es6
// error TS2569: Type ‘IterableIterator<string>’ is not an array type or a string type. Use compiler option ‘–downlevelIteration’ to allow iterating of iterators.
let map= new Map<string, string>();
map.set("a1","1");
map.set("a2","2");
map.set("a3","3");
map.forEach((value , key) =>{
if(value == 'a1'){
return;
}
console.log(key) // output: a1 a2 a3
});

for (const i of map.values()) {
if(i == '1'){
break;
}
console.log(i) // not run here
}
查看回复

回复 (1)

M
MaledongGit#1·5 年前

error TS2569: Type ' IterableIterator' is not an array type or a string type. Use compiler option ‘–downlevelIteration’ to allow iterating of iterators.

请检查你的TypeScript的target是不是es6?如果是es6以下版本,请这样做(在ts.config):

{
  "compilerOptions": {
    /* Basic Options */
     "downlevelIteration": true,
/* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
}
参与回复
登录后即可参与回复。登录