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

223
Views
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 answers
Answer question

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 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
Show me some job opportunities
There's an error!