"TypeError: can't convert undefined to object" when Dynamically Resizing
2D Array
I would like to read some text, consisting of triads of comma separated
numbers with one triad per line, into a 2D array. I do not know in advance
what the array dimensions will be. I use the following code.
// Read data into a matrix
var inputData = [[]];
while (allTextLines.length>0) {
dataRecord=allTextLines.shift();
entries = dataRecord.split(',');
var xCoord=parseInt(entries.shift());
var yCoord=parseInt(entries.shift());
var zCoord=parseInt(entries.shift());
if (yCoord>=inputData.length) inputData[yCoord]=[];
inputData[yCoord][xCoord]=zCoord;
}
This results in
TypeError: can't convert undefined to object
from Firebug when I try to call
if (yCoord>=inputData.length) inputData[yCoord]=[];
or
inputData[yCoord][xCoord]=zCoord;
I thought that JavaScript arrays could be dynamically resized by assigning
a value to an index higher than the current size.
No comments:
Post a Comment