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

91
Visualizações
Pushing to an array inside an object references the pushed object

The snippet is detailing my problem. I have an object that stores an array of objects (some of which are arrays). When I push to these internal arrays, that value pushes to all of the elements in the array instead of just the index I'm looking for.

I feel like this should be a really easy fix - but I've tried everything. Switching to concat,push,putting things into for loops. When I run a simplified version in the snippet - (see snippet below) - then it says there's a "ref" to a certain ID. I'm not sure what this means, but my guess is that someone does know! any help would be great.

const runs = {
  AC: {
    L: Array(10).fill({
      stim: [],
      mask: [],
      resp: [],
      other: []
    }),
    R: Array(10).fill({
      stim: [],
      mask: [],
      resp: [],
      other: []
    })
  },
}

let runData = runs["AC"]["R"][3]

runData.stim.push(40)

console.log(runs.AC.R)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are filling the array with the same reference. Instead, fill the array with empty items, then map to insert the object:

runs = {
  AC: {
    L: Array(10).fill().map(e=>({
      stim: [],
      mask: [],
      resp: [],
      other: []
    })),
    R: Array(10).fill().map(e=>({
      stim: [],
      mask: [],
      resp: [],
      other: []
    }))
  },      
}

let runData = runs["AC"]["R"][3]

runData.stim.push(40)

console.log(runs.AC.R)

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