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

136
Visualizações
How to iterate over the rows of a table with p5js using table.setNum()
function saveAsCSV() {
  
  let simulationOutput = new p5.Table();

  let newRow = simulationOutput.addRow();
  
  simulationOutput.addColumn("Total Population");
  simulationOutput.addColumn("Human Population");
  simulationOutput.addColumn("Predator Population");
  simulationOutput.addColumn("Prey Population");
  simulationOutput.addColumn("Food Population");

  for(var i = 1; i<totalDynamic.length; i++){
    simulationOutput.addRow().setNum("Total Population", int(totalDynamic[i]));
  }
  
  for(let j = 1; j<predDynamic.length; j++){
    simulationOutput.setNum(j,1, int(predDynamic[j]));
    j++;
  }
  
  save(simulationOutput, month()+"/"+day()+"_T:"+hour()+":"+minute()+":"+second()+"_Simulation_Output.csv");
}

I'm looking to save the outputs of a simulation to a .csv file, but I can't seem to iterate over the rows of the table using the for loop. I'm using p5.js

for(let j = 1; j<predDynamic.length; j++){
    simulationOutput.setNum(j,1, int(predDynamic[j]));
    j++;}

I get the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'setNum')
    at o.default.Table.setNum (p5.min.js:3:593463)
    at o.default.Element.saveAsCSV (predator_prey.js:255:22)
    at o.default.Element.<anonymous> (p5.min.js:3:429692)

The problem is with iterating using the j, but I can't see how this returns undefined?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You get this error because totalDynamic.length < predDynamic.length

A row is added for each item in totalDynamic and not predDynamic. But when there are more items in predDynamic than totalDynamic, The p5.Table() doesn't recognize the rows because they haven't been initialized yet. Possible solution:

for(let j = 1; j<predDynamic.length; j++){
   if(simulationOutput.rows.length <= j) simulationOutput.addRow();
   simulationOutput.setNum(j,1, int(predDynamic[j]));
   j++;
}
about 4 years ago · Santiago Trujillo 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