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

274
Views
Cómo actualizar objeto dinámico anidado javascript nodejs

Estoy tratando de actualizar dinámicamente los valores dentro de un objeto anidado como este:

 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' y 'Nested X' son nombres de propiedades dinámicas, quiero hacer un bucle en el objeto e intentar actualizar cada 'valor X' de cada objeto anidado.

Pero siempre obtengo el resultado de la última iteración para todos los objetos 'Test X'.

Aquí está mi código:

 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 } }

Actualizar

El mainObj lo obtengo de otra función (no de inicialización directa como en el ejemplo). En mi código:

 const mainObj = await processObj()

así, incluso la solución Math.random() (en las respuestas) no funciona, siempre obtengo los últimos valores de objetos anidados en todos los objetos anidados.

Actualización 2

Puedo consolar el registro de todos los valores correctamente con las claves, pero no puedo actualizarlo.

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

Así puedo ver todos los valores correctos: Prueba 1 - Anidado 1 - Valor de DB / Prueba 1 - Anidado 2 - Valor de DB, etc.

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

0

Saqué su dbquery, solo para mostrar que el acceso a los objetos dobles está bien:

 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();

da salida de:

 { '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

si puede ver esto también, entonces haría algo como esto, para asegurar que la asignación funcione bien desde la consulta de base de datos:

 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); }); } }
over 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
Virtual assistant