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

156
Views
Cannot update nested javascript object values with for loop

I get an object from a function after some process like this :

const mainObj = await processObj()

the object structure like :

{
  'Test 1': {
    Nested1: 'value 1',
    Nested2: 'value 2',
    Nested3: 'value 3',
    Nested4: 'value 4',
    Nested5: 'value 5'
  },
  'Test 2': {
    Nested1: 'value 1',
    Nested2: 'value 2',
    Nested3: 'value 3',
    Nested4: 'value 4',
    Nested5: 'value 5'
  },
  'Test 3': {
    Nested1: 'value 1',
    Nested2: 'value 2',
    Nested3: 'value 3',
    Nested4: 'value 4',
    Nested5: 'value 5'
  }
}

'Test X' and 'Nested X' are dynamic property names, I want to loop the object and try to update each 'value X' of each nested object.

I can acccess the values but I cannot update, the following does show the result :

for (const x in mainObj) {
      for (const y in mainObj[x]) {
          const count = await dbquery(x,y)
          console.log(`${x} ${y} ${count}`)
      }
}

But when I try to update, it doesn't work :

for (const x in mainObj) {
      for (const y in mainObj[x]) {
          const count = await dbquery(x,y)
          mainObj[x][y] = count
      }
}

When I console.log(mainObj), I get the last iteration results in all nested objects.

I think It's a reference issue, and need to use Object.Assign() but I don't have ideas how

about 4 years ago · Juan Pablo Isaza
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!