My dataset is structured as a nested JSON, with the main object named bwaResult. Within this object, there are three primary groups: actBwa, fcBwa, and planBwa. Each of these groups contains yearly data and results that include years. I am trying to organize this data by assigning values to specific years, such as 2020, for each group.
In my code snippet:
const plMonthResults = {};
const plMonth = resp.success ? resp.bwaResult : null; // JSON-DATA
delete resp.success;
if (plMonth && plMonth !== null && Object.keys(plMonth)) {
let count = 0;
for (const bwaType in plMonth) {
const plMonthData = plMonth[bwaType]; // Extracting actBwa, fcBwa, planBwa
if (count === 0) {
for (const yearKey of Object.keys(plMonthData)) {
plMonthResults[yearKey] = {}; // Organizing data by years
}
}
for (const yearKeyInPlMonth in plMonthData) {
plMonthResults[yearKeyInPlMonth][bwaType] = plMonthData[yearKeyInPlMonth]; // Error occurs here
}
count += 1;
}
}
Unfortunately, when running the code, I encounter the following error:
ERROR TypeError: Cannot set properties of undefined (setting 'fcBwa')
I suspect that the issue arises from missing values for certain years within the different groups like fcBwa. Can someone guide me on how to resolve this issue?
Check out my progress on StackBlitz: https://stackblitz.com/edit/read-local-json-file-service-udqvck?file=src%2Fapp%2Fapp.component.ts