Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
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...

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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);
            });
    }
  }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda