Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda