简体   繁体   English

如何使用 JavaScript 将值推送到嵌套数组中?

[英]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>

Hello and welcome to stackoverflow Jacob.您好,欢迎来到 stackoverflow Jacob。 The issue seems to be at this line of code data.datasets[3].data.push(randNum);问题似乎出在这行代码data.datasets[3].data.push(randNum); . . You are accessing the datasets property which has an array as a value.您正在访问以数组作为值的datasets属性。 You are trying to access index 3 in this array which does not exist.您正在尝试访问此数组中不存在的索引 3。 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);仔细检查后,我们发现该数组仅包含一个元素,即 object。因此,您应该这样做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.它允许您访问数组中索引为 0 的第一个元素。我希望这是有道理的。

Using the Array constructor you can create nested arrays (There are other ways as well).使用Array构造函数,您可以创建嵌套的 arrays(还有其他方法)。

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM