I could use some guidance on specifying a dynamic multi dimensional array in javascript. I understand that javascript does not natively define multi dim arrays, but rather an array of an array, ie.
var items = [[1,2],[3,4],[5,6]];
or
var array = [[,],[,]]
or
var a = [[1,2],[3,4]]
My issue is that I do not know the actual dimensions, and simply defining the array, as in the second example above, still does not allow for the array to go beyond two record sets. I thought there was a REDIM stmt similar to VB but have not been able to locate anything.
One additional part of my problem is that when I specify the second dimension of the array, as in the example below, the array becomes unreachable outside of the for block.
var Exforsys=new Array(4)
for (i=0; i <4; i++) {
Exforsys[i]=new Array(4)
}
I am trying to retrieve data from my specific array like this…
function newTest() {
var myArray = [[],[]];
// myArray[] = new Array(14);
var recCount = coordsAry.length / 15;
var n =0;
var i = 0;
var s = 0;
for (i = 0; i < recCount; i++) {
for (s = 0; s < 15; s++) {
// myArray[i] = new Array(14);
myArray[i][s] = coordsAry[n];
n++;
// alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
}
// var u = 4;
s = 0;
alert(myArray[0][3]);
alert(myArray[0][4]);
alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
}
coordsAry has 105 records and is flat, {“12″,”Frank”,”564″, … etc} this has been proven. After the 2nd record is populated I get this error….
Error: Unable to set value of the property '0': object is null or undefined.
Obviously, my javascript skills are a bit rough to say the least. Any sample code would be greatly appreciated.
Thanks,
You do not Dim the Array to an actual size,
You can just set an variable on any index to whatever you want
if you want two Dimensions you just could define an array
var arr = []set an element to another array
arr[0] = []and put an value inside an element in the “2nd dimension”
arr[0][10] = 10You only have the elements in the “first dimension” to be an array
You could also do things like placing arrays into arrays
like
And if you have an array and want to push elements in an inner array just do a check if the element is defined, like
Given the Code you posted
This works for me