简体   繁体   English

循环创建数组+循环增量?

[英]Loop to create array + loop increment?

I wanted to create a function which gets called upon-page load and generates a graph based on what user inputs (multiple datasets, using chart.js ). 我想创建一个调用页面加载并根据用户输入(使用chart.js的多个数据集)生成图形的函数

Decided to rely on prompt() for initial testing, but soon ran into a problem. 决定依赖于hint()进行初始测试,但很快遇到了问题。

function defineDatasets(itt){
    for(i=0; i<itt; i++){
        var dataSet+i=[]; // <--- Does not result in a array called "dataSet0[], ..." etc
    }
}

So my question is, if and how could this be achieved? 所以我的问题是,是否以及如何实现?

If you're trying to suffix your arrays with an index, why not just make an array of arrays? 如果您要为数组添加索引后缀,为什么不仅仅制作数组数组呢? Also be sure to define your dataSet outside the function scope: 另外,请确保在函数范围之外定义dataSet

let dataSet = [];

function defineDatasets(itt) {
    for(var i = 0; i < itt; i++) {
        dataSet.push([]);
    }
}

console.log(dataSet[0]); // []

Now instead of referencing these arrays with dataSetN , you can do dataSet[N] , where N is the Nth dataSet index. 现在,可以不用dataSetN引用这些数组, dataSetN可以执行dataSet[N] ,其中N是第N个dataSet索引。

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

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