我是 typescript 中的新蜜蜂,在我的 angular4 项目中,我收到了一个 json 格式的 map 对象。
所以我声明了一个下面给出的类
<importing... required classes here..>
export class FormConfig {
public id: number;
public name: string;
public fieldMap :Map<string,Array<Field>>;
public fieldList : Array<Field> ;
}
我从 api 收到完全相同的 JSON。我安慰了 FormConfig 的对象,我得到了没有任何错误的控制台。我的代码编译时没有任何警告和错误。但是我无法使用 Map 的内置函数(如 forEach、keys、get 等)从 map 访问键和值。但是 IDE 显示了这些函数建议。下面给出了我的部分代码。
formConfig : FormConfig;
console.log(JSON.stringify(formConfig)); // works fine
keys : string [] = Array.from (formConfig.keys()) ; // showing error (formConfig.keys is not a function)
我正在为我的 angular4 项目使用 es5 目标,IDE 是 visual studio 代码。
在让我知道我在这里犯的错误之前,是否有人遇到过这个问题。
谢谢。
最佳答案
您可以像这样使用键获取值:
for (const key in formConfig) {
console.log('The value for ' + key + ' is = ' + formConfig[key]);
}
获取对象的键值数组是这样完成的:
keys: string [] = Object.keys(formConfig);
关于angular - 使用其函数时 Typescript Map 抛出错误(mapobject.keys() 不是函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45235007/