Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Length of Multiple Array

Status
Not open for further replies.

thegentleman

IS-IT--Management
Apr 4, 2001
65
GB
Hi,

I am creating a function that requires large arrays of products split into 4 levels:

ar[0] = new Array();
ar[0][0] = new makeOption("NONE","0");
ar[1] = new Array();
ar[1][0] = new makeOption("NONE","0");
ar[2] = new Array();
ar[2][0] = new makeOption("NONE","0");
ar[3] = new Array();
ar[3][0] = new makeOption("NONE","0");
ar[3][1] = new makeOption("James Bond","11");
ar[3][1][0] = new makeOption("NONE","0");
ar[3][1][1] = new makeOption("40th Anniversary","18");
ar[3][1][1][0] = new makeOption("NONE","0");
ar[3][1][2] = new makeOption("Anniversary Expansion","19");
ar[3][1][2][0] = new makeOption("NONE","0");

I can assign the length of the first level arrays to a variable without any problem using the following code:

var curAr = ar[3].length

But I cannot seem to get the length of the second level array with the following:

var curAr = ar[3][1].length

I do not get an error but in this scenario curAr is always 0 or Null. Here is the full function:

-----------------------------------------------

function relate_s1(form) {
var options = form.cid_s2.options;
for (var i = options.length - 1; i > 0; i--) {
options = null;
}
var curAr = ar[form.cid.selectedIndex][form.cid_s1.selectedIndex];
for (var j = 0; j < curAr.length; j++) {
options[j] = new Option(curAr[j].text,curAr[j].value);
}
options[0].selected = true;

}

-------------------------------

Any ideas?

~tg
 
But I cannot seem to get the length of the second level array...

There is no second level array defined. I think it may have something to do with the following snippet from your code:

Code:
ar[3] = new Array();
ar[3][0] = new makeOption(&quot;NONE&quot;,&quot;0&quot;);

I think that by setting ar[3][0] to something, you revoke any attempt on the previous to create a new array.

The following code snippet sets up a similar set of arrays and alerts some information about the length of them:

Code:
<script type=&quot;text/javascript&quot;>
var myArray = new Array();

myArray[0] = new Array();
myArray[0][0] = new Array();
myArray[0][1] = new Array();
myArray[1] = new Array();
myArray[1][0] = new Array();
myArray[2] = new Array();
myArray[2][0] = new Array();
myArray[2][0][0] = &quot;Array : Option 2 : Option 0 : Option 0&quot;;
myArray[2][0][1] = &quot;Array : Option 2 : Option 0 : Option 1&quot;;
myArray[2][0][2] = &quot;Array : Option 2 : Option 0 : Option 2&quot;;
myArray[2][1] = &quot;Array : Option 2 : Option 1&quot;;

alert(myArray[0].length); // 2
alert(myArray[1].length); // 1
alert(myArray[2].length); // 2
alert(myArray[0][0].length); // 0
alert(myArray[2][0].length); // 3
alert(myArray[2][1].length); // 27
</script>

Maybe if you looked at my code snippet and then applied some of the ideas to your own... you will find a solution that suits.

See how you go... there are plenty of people here that can offer suggestions... mine may not be the most appropriate for your needs.

Jeff
 
Thanks for the input Jeff.

I adjusted the code to reflect your first point and the code now looks like this:

ar[0] = new Array();
ar[0][0] = new Array();
ar[0][1] = new makeOption(&quot;NONE&quot;,&quot;0&quot;);
ar[1] = new Array();
ar[1][0] = new Array();
ar[1][1] = new makeOption(&quot;NONE&quot;,&quot;0&quot;);
ar[2] = new Array();
ar[2][0] = new Array();
ar[2][1] = new makeOption(&quot;NONE&quot;,&quot;0&quot;);
ar[3] = new Array();
ar[3][0] = new Array();
ar[3][1] = new makeOption(&quot;NONE&quot;,&quot;0&quot;);
ar[3][2] = new makeOption(&quot;James Bond&quot;,&quot;11&quot;);
ar[3][2][0] = new Array();
ar[3][2][1] = new makeOption(&quot;NONE&quot;,&quot;0&quot;);
ar[3][2][2] = new makeOption(&quot;40th Anniversary&quot;,&quot;18&quot;);
ar[3][2][2][0] = new Array();
ar[3][2][2][1] = new makeOption(&quot;NONE&quot;,&quot;0&quot;);

But it still isn't working. I may have got myself confused here but from what you say does it mean that I cannot have an item in both 'ar[3][2]' and 'ar[3][2][0]' in the array because if I have something in 'ar[3][2][0]' then 'ar[3][2]' has to define the next level array?

I really appreciate the help.

~tg
 
Any further help with this would be greatly appreciated.

~tg
 

tg,

You are correct - having something in ar[3][2] that wasn't an array would mean that ar[3][2][0] wouldn't exist.

The 'parent' of any array element must be an array.

Hope this clarifies things.

Dan
 
Thanks Dan,

I eventually worked out a way around this. The reason why it got so complicated is because this is all being generated from a database.

var l1 = new Array();
var l2 = new Array();
var l3 = new Array();

l1[0] = new Array();
l1[1] = new Array();
l1[2] = new Array();
l1[3] = new Array();
l1[4] = new Array();
l1[5] = new Array();
l1[6] = new Array();
l1[7] = new Array();

l1[0][0] = new makeOption(&quot;NONE&quot;,&quot;0&quot;);
l1[0][1] = new makeOption(&quot;01&quot;,&quot;0&quot;);

l2[0] = new Array();
l2[0][1] = new Array();

l2[0][1][0] = new makeOption(&quot;NONE&quot;,&quot;0&quot;);
l2[0][1][1] = new makeOption(&quot;011&quot;,&quot;0&quot;);

l3[0] = new Array();
l3[0][1] = new Array();
l3[0][1][1] = new Array();

l3[0][1][1][0] = new makeOption(&quot;NONE&quot;,&quot;0&quot;);
l3[0][1][1][1] = new makeOption(&quot;0111&quot;,&quot;0&quot;);

l1[1][0] = new makeOption(&quot;NONE&quot;,&quot;0&quot;);
l1[1][1] = new makeOption(&quot;11&quot;,&quot;0&quot;);

This code evntually leads to 4-tear dependant drop-down lists. Thanks for all the help.

~tg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top