• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

169
Views
How to update nested dynamic object javascript nodejs

I'm trying to dynamically update values inside a nested object like this :

mainObj = {
  '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.

But I always get the last iteration result for all 'Test X' object.

Here is my code :

  for (const [key, value] of Object.entries(mainObj)) {
    for (const [keyEm, valueEm] of Object.entries(mainObj[key])) {
      const count = await dbquery(key, keyEm)
      mainObj[key][keyEm] = count
    }
  }

Update

The mainObj I get it from another function (not direct initialization like the example). In my code :

const mainObj = await processObj()

like this even the Math.random() solution (in the answers) is not working, I always get the last nested object values in all nested objects.

Update 2

I can console log all values correctly with keys but I can't update it.

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

Like this I can see all the values correct : Test 1 - Nested 1 - Value from DB / Test 1 - Nested 2 - Value from DB etc...

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I took out your dbquery, just to show that the access to the double objects is ok:

function foo() {
    return mainObj;
}
const mainObj = {
    'Test 1': {
        Nested1: 1,
        Nested2: 2,
        Nested3: 3,
        Nested4: 4,
        Nested5: 5,
    },
    'Test 2': {
        Nested1: 1,
        Nested2: 2,
        Nested3: 3,
        Nested4: 4,
        Nested5: 5,
    },
    'Test 3': {
        Nested1: 1,
        Nested2: 2,
        Nested3: 3,
        Nested4: 4,
        Nested5: 5,
    },
};
const bar = foo();
async function demo() {
    console.log(mainObj);
    for (const [key, value] of Object.entries(bar)) {
        for (const [keyEm, valueEm] of Object.entries(bar[key])) {
            // const count = await dbquery(key, keyEm);
            bar[key][keyEm]++;
            console.log(bar[key][keyEm]);
        }
    }
}
demo();

gives output of:

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

if you can see this too, then I would do something like this, to ensure the assignment works well from the db query:

  for (const [key, value] of Object.entries(mainObj)) {
      for (const [keyEm, valueEm] of Object.entries(mainObj[key])) {
        await dbquery(key, keyEm)
            .then(count => {
                mainObj[key][keyEm] = count;
            })
            .catch(err=> {
                 console.error(err);
            });
    }
  }
almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error