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

316
Views
Creating multiple object instances in for loop JavaScript

I am a bit struggling to understand this issue: There are two plots with two different data passed in y: data as you can see at the first snippet below, instead of manually writing all the plots like in it, and since my original data have an arbitrary length (1, N), is there a clever way to define the plots in the for loop?

This is a dummy but working version.

Plotly.newPlot("plot", [{
  y: [getData()], // calls Math.random()
  type: 'line',
  line: {
    color: 'rgb(142, 142, 142)',
    width: 1
  },
}, {
  y: [getSData()], // calls Math.random()
  type: 'line',
  line: {
    color: 'rgb(142, 142, 142)',
    width: 1
  },
}

], layout, config);

I have tried to convert this snippet to with the for loop version, as I mentioned above my data has an arbitrary length [[1], ..., [N]].

let ob;
let PLOTS = Plotly.newPlot("plot", ob, layout, config)

// console.log(pos) => [Array(1)] 

for (let i = 0; i < pos.length; i++){
  ob.y = pos[I] // this spits an error
  ob.type = 'line'
  ob.color =  'rgb(142, 142, 142)'
}

let cnt = 0

setInterval(function(){
  Plotly.extendTraces('plot', {y: pos}, [...Array(pos.length).keys()]);

  cnt ++

  if (cnt > 128) {

    Plotly.relayout("plot", {
      xaxis: {
        range: [cnt-128, cnt]
      }
    });
  }

}, 20);

I am getting an error Uncaught TypeError: Cannot set properties of undefined (setting 'y') at ob.y = pos[i] I am not familiar with the language and the errors, therefore I might need a bit help. Thanks.

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