简体   繁体   中英

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

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:

 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:

 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM