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

221
Visualizações
How can I create a list of objects dinamically in JavaScript?

I'm trying to create an object then put it into an array. But (I guess) when I create a new object the objects inside of the array is getting modified as well, so all the array is getting the same object. Is something like the code below. How can I make an array of differents objects?

var randomCode = []
var fligthRange = []
var maintenanceArr = []
var results = {ID: '', range: '', maintenance: ''} //the object
var finalResults = [] //the array of objects
var counter = 0

function send(){     //this function is called when a button is clicked on html
  results.ID = randomCode[counter]
  results.range = fligthRange[counter]
  results.maintenance = maintenanceArr[counter]
  finalResults[counter] = results
  counter++
}

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

0

Just build the object literal in the function itself:

function send(){
  finalResults[counter] = {
    ID: randomCode[counter], 
    range: fligthRange[counter], 
    maintenance: maintenanceArr[counter]
  }
  counter++
}

You will get a new instance added to the array every time the function is called.

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