I have a nested JSON, this is structured as follows:

As you can see the JSON is called bwaResult and in this object there are three big groups. These groups are called actBwa, fcBwa and planBwa. These groups contain years and yearlyResults(sums) which also have years. I would like to assign the values to the individual years. That means in 2020 you would have actBwa, fcBwa, planBwa and the yearlyResults values and the same for the other years.
My Code:
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]; // Here I get actBwa, fcBwa, planBwa
if (count === 0) {
for (const yearKey of Object.keys(plMonthData)) {
plMonthResults[yearKey] = {}; // Sort by years
}
}
for (const yearKeyInPlMonth in plMonthData) {
plMonthResults[yearKeyInPlMonth][bwaType] = plMonthData[yearKeyInPlMonth]; // Here then arises the error
}
count += 1;
}
}
When I run the code then I get the following error:
ERROR TypeError: Cannot set properties of undefined (setting 'fcBwa')
I guess since actBwa only has 2020 and the other groups have more years there is a problem finding fcBwa and iterating further or am I seeing this wrong? Can you tell me how to solve this problem?
UPDATE my work in stackblitz: https://stackblitz.com/edit/read-local-json-file-service-udqvck?file=src%2Fapp%2Fapp.component.ts