Skip to content
Advertisement

Convert json from file to map not working

i have this code which gives the error “Error: Uncaught (in promise): TypeError: this.weekMap.get is not a function”. Why weekMap is not recognized as a Map object?

let weekMap: Map<string, ExerciseDay>;
 let jsonWeekMap =require('./initialWeekMap.json');
  weekMap=jsonWeekMap as Map<string, ExerciseDay>;
  console.log("week data got:"+weekMap.get("Monday"))

The Json file contains:

{
"Monday":{
    "englishDayName":"Monday",
    "dayNameTranslated":"Monday",
    "counter":0,
    "exerciseList":[]
},
"Tuesday":{
    "englishDayName":"Tuesday",
    "dayNameTranslated":"Tuesday",
    "counter":0,
    "exerciseList":[]
},
"Wednesday":{
    "englishDayName":"Wednesday",
    "dayNameTranslated":"Wednesday",
    "counter":0,
    "exerciseList":[]
},
"Thursday":{
    "englishDayName":"Thursday",
    "dayNameTranslated":"Thursday",
    "counter":0,
    "exerciseList":[]
},
"Friday":{
    "englishDayName":"Friday",
    "dayNameTranslated":"Friday",
    "counter":0,
    "exerciseList":[]
},
"Saturday":{
    "englishDayName":"Saturday",
    "dayNameTranslated":"Saturday",
    "counter":0,
    "exerciseList":[]
},
"Sunday":{
    "englishDayName":"Sunday",
    "dayNameTranslated":"Sunday",
    "counter":0,
    "exerciseList":[]
}

}

Advertisement

Answer

You are not converting the variable weekMap to a Map object correctly. Try the Object.entries function:

weekMap = new Map<string, ExerciseDay>(Object.entries(jsonWeekMap));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement