Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
Cannot set properties of undefined by iteration the nested JSON

I have a nested JSON, this is structured as follows: enter image description here

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It seems like you're using count to copy the keys only once, but that means you will only have the keys of the first object (in this case actBwa). You can instead go through the key of each object, but make sure you don't override the existing ones. So remove the count and modify the look like this

       for (const yearKey of Object.keys(plMonthData)) {
          plMonthResults[yearKey] = plMonthResults[yearKey] ?? {};
        }

You can even remove this loop and include the check into the main one:

   for (const yearKeyInPlMonth in plMonthData) {
          plMonthResults[yearKeyInPlMonth] = plMonthResults[yearKeyInPlMonth] ?? {};
          plMonthResults[yearKeyInPlMonth][bwaType] = plMonthData[yearKeyInPlMonth]; 
        }

Let me know if that gives you the result you're after

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!