I have a task to convert my JSON data from its current format as shown below:
cacheMapDataDto = [{
"cacheName": "cache_nchl_individual_type",
"count": 2,
"mapObj": {
"NCHL_BI_BATCH_VERIFICATION": false,
"NCHL_STL_BATCH_VERIFICATION": false
},
"nameOfCache": "NCHL Verification Status"
}]
To the desired format specified below:
{"cacheName":"cache_member_dto_type",
"count":1,
"mapOfDto":[{"id":merCode,"value":"1"},{"id":merName,"value":"DE"},{"id":merId,"value":"10"}]
"nameOfCache":"Member DTO"
};
In order to achieve this, I attempted to convert the mapOfDto to key/value pairs using the following method:
formatData(cacheMapDataDto:any){
console.log("abc");
console.log(cacheMapDataDto);
this.result = Object.keys(cacheMapDataDto.mapOfDto).map(function(key) {
return this.result[key] =cacheMapDataDto.mapOfDto[key], this.result;
});
console.log(this.result);
this.cacheNewData={
"cacheName":cacheMapDataDto.cacheName,
"count":cacheMapDataDto.count,
"mapOfDto":this.result,
"nameOfCache": cacheMapDataDto.nameOfCache
};
console.log(this.cacheNewData);
}
However, I encountered an error stating:
TypeError: Cannot convert undefined or null to object
at Function.keys (<anonymous>)
at CacheMetaDataComponent.formatData
The source of the error seems to be in this particular line of code within the TypeScript file:
this.result = Object.keys(cacheMapDataDto.mapOfDto).map(function(key) {
return this.result[key] =cacheMapDataDto.mapOfDto[key], this.result;
});
Do you have any suggestion for a better approach?