Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda