Saturday 15 February 2014

javascript - Assigning objects in 2-D array -


Assume that I have an object as follows:

  function node (xVal , YVal, node type) {this.xVal = xVal; // x-coordinate of node this.yVal = yVal; Coordinate of the / y-node this.nodeType = nodeType; // node type - out of the scope of this question}   

In an attempt to create a series of nodes on the X-by-Y virtual plane, I have specified the two-dimensional array as follows : var ROWS = 3; // Number of rows in the table var COLS = 10; // table var nodes = number of columns in new array (ROWS); (Var i = 0; i & lt; ROWS; i ++) {for (var j = 0; j & lt; COLS; j ++) {nodes [i] [j] = new node (0, 0, "Type A"); }}

I was hoping that the embedded for-loop above would allow me to initialize the 3x10 array, with each 'node' object, but some error is occurring . No idea as 1) what error can arise, and 2) how to improve the argument will be greatly appreciated!

You must also define an internal array.

 for  (var i = 0; i & lt; ROWS; i ++) {nodes [i] = new array (CLS); For (var j = 0; j & lt; cols; j ++) {nodes [i] [j] = new node (0, 0, "type a"); }}    

No comments:

Post a Comment