创建P5.js二维数组 [英] Creating a P5.js 2-Dimensional Array
问题描述
我正在尝试将我的工作处理草图移植到p5.js,以便我可以在线使用它。但是当我尝试在二维数组中设置值时,我遇到了一个问题。我想我正在遵循API的2d数组部分中显示的正确语法,但是我得到一个未被捕获的类型错误,上面写着无法设置未定义的属性0。
I'm trying to port my working processing sketch to p5.js so I can use it online. But I'm running into a problem when trying to set the values in a 2 dimensional array. I think I'm following the correct syntax shown in the 2d array section of the API, but am getting an uncaught type error that says "cannot set property 0 of undefined".
草图非常简单,我只是想从CSV中读取并将数据存储在2D数组中:
The sketch is pretty simple, I'm just trying to read from a CSV and store the data in a 2D array:
var cols = 8;
var rows = 8;
var month = 1;
var values = [];
var table;
function preload(){
table = loadTable("1988.csv", "csv");
}
function setup() {
createCanvas(800, 800);
var totalRows = table.getRowCount();
for (var m = 0; m < 12; m++) {
for (var c = 0; c < cols; c++) {
values[c]=[];
for (var r = 0; r < rows; r++) {
values[r+m*rows][c] = table.getNum(m*rows*cols + c*rows + r, 0);
}
}
}
}
任何非常感谢帮助!
感谢您的时间。
推荐答案
尝试在将数组访问值设置为 undefined
对象之前分配数组:
Try allocating the array before setting array access values to undefined
objects:
var cols = 8;
var rows = 8;
var month = 1;
var months = 12;
var values = new Array(months);
var table;
function preload(){
table = loadTable("1988.csv", "csv");
for(var i = 0 ; i < months; i++) values[i] = [];//initialize 2nd dimension of the array as well, not just the 1st
}
function setup() {
createCanvas(800, 800);
var totalRows = table.getRowCount();
for (var m = 0; m < 12; m++) {
for (var c = 0; c < cols; c++) {
values[c]=[];
for (var r = 0; r < rows; r++) {
values[r+m*rows][c] = table.getNum(m*rows*cols + c*rows + r, 0);
}
}
}
}
这篇关于创建P5.js二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!