Typescript Map throwing error while using its functions (mapobject.keys() is not a function)
You can get the values using key like this:
for (const key in formConfig) {
console.log('The value for ' + key + ' is = ' + formConfig[key]);
}
And getting array of the Object's key values is done like this:
keys: string [] = Object.keys(formConfig);
Related videos on Youtube
![Vishnu KR](https://i.stack.imgur.com/b1FV3.jpg?s=256&g=1)
Vishnu KR
Updated on June 06, 2022Comments
-
Vishnu KR about 2 years
I am a new bee in typescript , In my angular4 project I am receiving a map object as a json.
so I declared a class that is given bellow
<importing... required classes here..> export class FormConfig { public id: number; public name: string; public fieldMap :Map<string,Array<Field>>; public fieldList : Array<Field> ; }
I am receiving exactly same JSON from api. I consoled the object of FormConfig and I am getting the console without any error. My code got compiled without any warning and error. But I can't access the keys and values from the map by using Map's inbuilt functions like forEach,keys,get etc. But IDE showing these function suggestions. Part of my code is given bellow.
formConfig : FormConfig; console.log(JSON.stringify(formConfig)); // works fine keys : string [] = Array.from (formConfig.keys()) ; // showing error (formConfig.keys is not a function)
I am using es5 target for my angular4 project and the IDE is visual studio code.
Is anybody faced this issue before let me know the mistake I have made here.
Thank you.
-
onetwo12 almost 7 yearsDid you try
keys : string [] = Object.keys(formConfig)
?
-
-
Vishnu KR almost 7 yearsThank you very much man.. but why this get, foreach and keys function not working ? do you have any idea ?
-
onetwo12 almost 7 yearsIf you want to use
foreach
you have to have mapMap.prototype.forEach()
, but in your case it'sObject
and for that you use for .. in