Let's say I got an object as follows:
function node(xVal, yVal, nodeType) {
this.xVal = xVal; // x-coordinate of node
this.yVal = yVal; // y-coordinate of node
this.nodeType = nodeType; // node type - outside the scope of this question
}
In an attempt to create a series of nodes on a x-by-y virtual plane, I specified a two-dimensional array as follows:
var ROWS = 3; // number of rows in the array
var COLS = 10; // number of columns in the array
var Nodes = new Array(ROWS);
for (var i=0; i < ROWS; i++) {
for (var j=0; j < COLS; j++) {
Nodes[i][j] = new node(0, 0, "Type A");
}
}
I was expecting the above embedded for-loop would allow me to initialize the 3x10 array, each with 'node' object but something appears to be causing an error. Any thoughts as to 1) what might be causing the error, and 2) how to improve the logic will be greatly appreciated!
Nodes[i]is not defined. You have to initialize it with a value first, i.e. the "second level" array.