[英]TypeScript: Cannot get values or key from Map
my problem is that when I'm trying to access map keys or values by method i still get error:我的问题是,当我尝试通过方法访问地图键或值时,我仍然收到错误:
ERROR TypeError: factors.values is not a function or its return value is not iterable
错误类型错误:factors.values 不是函数或其返回值不可迭代
These are model interfaces that I use for object types:这些是我用于对象类型的模型接口:
export interface Plant{ name: string; templateName: string; } export interface RiskFactor { riskId: number; name: string; slotName: string; factorType: string; plantType: string; } export interface Symptom { symptomId: number; name: string; slotName: string; plantType: string; checked: boolean; } export interface DiagnoseForm { riskFactors: Map<string, RiskFactor[]>; symptoms: Symptom[]; }
I save server response with DiagnoseForm to "formData" variable, and I can access and create formArray from symptoms normally:我使用 DiagnoseForm 将服务器响应保存到“formData”变量,我可以正常访问和从症状创建 formArray:
buildSymptoms(){ const arr = this.formData.symptoms.map(symptom => { symptom.checked = false; return this.formBuilder.control(false); }); return this.formBuilder.array(arr); }
When I try to get these riskFactors arrays from map I get error from above to the following code:当我尝试从地图中获取这些 riskFactors 数组时,我从上面得到以下代码的错误:
buildRiskFactors(){ const factors: Map<string, RiskFactor[]> = this.formData.riskFactors; for( let wrapper of factors.values()){ console.log(wrapper); } }
I've tried to solve this in many ways but each and every one give the same output.我试图以多种方式解决这个问题,但每一种都给出相同的输出。
你应该做 Object.values(factors) 而不是 factor.values()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.