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

155
Visualizações
How can I push values into nested array with JavaScript?

I have the following code and I'm attempting to push randomly generated numbers into the data array. I'm just not quite sure on how to access the array. I know my code is probably very sloppy and inefficient, but I'm trying my best to learn and got stuck on this. I'd really appreciate any help.

<script>
  var balance = 0;

  function nextDay() {
    var dayCount = 0;
    if (dayCount < 5) {
      randNum = Math.random() * (max - min) + min;
      console.log(randNum);
      max = randNum + 50;
      min = randNum - 50;
      dayCount++;
    }
  }

  function sell() {
    balance = balance + randNum;
    console.log("current balance:" + balance);
    randNum = 0;
    max = randNum + 50;
    min = randNum - 50;
  }

  const startingValues = [];
  function getStartingValues() {
    var max = 100;
    var min = -100;
    for (let startingCount = 0; startingCount < 8; startingCount++) {
      var randNum = Math.random() * (max - min) + min;
      max = randNum + 500;
      min = randNum - 100;
      data.datasets[3].data.push(randNum);
      console.log(startingValues[startingCount]);
    }
  }
  getStartingValues();

  const labels = ["Week 1", "Week 2", "Week 3", "Week 4", "Week 5", "Week 6"];

  const data = {
    labels: labels,
    datasets: [
      {
        label: "graph",
        backgroundColor: "rgb(255, 99, 132)",
        borderColor: "rgb(255, 99, 132)",
        data: [],
      },
    ],
  };

  const config = {
    type: "line",
    data: data,
    options: {},
  };
</script>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Hello and welcome to stackoverflow Jacob. The issue seems to be at this line of code data.datasets[3].data.push(randNum);. You are accessing the datasets property which has an array as a value. You are trying to access index 3 in this array which does not exist. On closer inspection we realize that the array contains only one element which is an object. So, instead you should be doing this data.datasets[0].data.push(randNum); which allows you to access the first element in the array which is at index 0. I hope that makes sense.

about 4 years ago · Juan Pablo Isaza Relatório

0

Using the Array constructor you can create nested arrays (There are other ways as well).

  const data = [];
  const startingValues = [];
  
function getStartingValues() {
    var max = 100
    var min = -100
    for (let i = 0; i <= 7; i++) {

        data[i] = Math.random() * (max - min) + min;
    }
    //Add data as nested arrray to startingValues 
    startingValues[0] = new Array(data);
}

getStartingValues();
console.log(startingValues); //Starting values is a 1x8 nested array

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