Home > front end >  How can I push values into nested array with JavaScript?
How can I push values into nested array with JavaScript?

Time:02-12

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.

        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>

CodePudding user response:

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.

CodePudding user response:

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

<script>

  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

</script>

  • Related