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

318
Visualizações
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
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