[英]typescript/angular: can't iterate over Map parsed from json
I'm new to both typescript and angular (coming from quite solid Java/kotlin background).我是 typescript 和 angular 的新手(来自相当扎实的 Java/kotlin 背景)。 Wrote a class:
写了一个类:
export interface GeoData {
allregions: Map<string, string>;
}
to parse this json:解析这个json:
{"allregions":{"whatever":"WHT","a region":"ARE","something":"SMT"}
The json is correctly read from a file using HttpClient.get()
and I can see the correct content in the variable using debug.使用
HttpClient.get()
从文件中正确读取了 json,我可以使用 debug 查看变量中的正确内容。 also, this code:另外,这段代码:
console.log(data.allregions["whatever"])
correctly prints WHT
.正确打印
WHT
。
unfortunately, this:不幸的是,这:
data.allregions.forEach((value: string, key: string) => {
console.log(key, value);
});
throws data.allregions.forEach is not a function
抛出
data.allregions.forEach is not a function
also this:还有这个:
console.log(data.allregions.size)
prints undefined
打印
undefined
and this:和这个:
console.log(data.allregions.entries.lenght)
throws data.allregions.entries is undefined
抛出
data.allregions.entries is undefined
what is going on here?这里发生了什么?
I see you are applying forEach on a object.我看到你在一个对象上申请 forEach。 Combine Object.keys() and forEach()
结合Object.keys()和forEach()
var data = {"allregions":{"whatever":"WHT","a region":"ARE","something":"SMT"}} Object.keys(data.allregions).forEach(function(key) { console.log(`key: ${key}, value:${data.allregions[key]}`); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.