...

View Full Version : using contents of one array as index into another



prism
03-11-2005, 09:23 PM
I'm having trouble using the contents of an array as an index into another, for example I have an array1 which holds the values 4,5,6. and I want to access array2[4] array2[5] array2[6].

The code I've been trying to use is something like:



for(i = 1; i < 3; i++)
{
alert(array2[ array1[i] ]);
}


I've also tried assigning the contents of array1 to another variable 'j' and using that as the index.



for(i = 0; i < 3; i++)
{
j = array1[i];
alert(array2[j]);
}


If anybody can help I would greatly appreciate it.

Thanks.

Mr J
03-11-2005, 11:34 PM
How about this


array1=new Array()
array1[0]=4
array1[1]=5
array1[2]=6

array2=new Array()
array2[0]="a"
array2[1]="b"
array2[2]="c"
array2[3]="d"
array2[4]="e"
array2[5]="f"
array2[6]="g"

for(i = 0; i < 3; i++){
alert(array2[ array1[i] ])
}

prism
03-12-2005, 03:15 PM
Thanks for the reply.

I have a cookie string, which looks something like:

cookie_name=,1,2,3;

I split it at the ',' because I just want the numbers 1,2,3 or whatever I've stored in it. I then want to use these numbers as an index into an array.

so I'm using something like

numbers = document.cookie.split(',');

numbers[0] will be cookie_name
numbers[1] will be 1
numbers[2] will be 2, etc.

so I then want to access an array using the numbers which are stored in numbers[1] and >.

My array is something like:

var array1 = new Array(10);

array1[0] = "zero";

array1[1] = "one";
....
....

but If I try to use something like:

alert(array1[ numbers[1] ]);

It tells me that the value is undefined



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum