Javascript - what array to look at

Associate
Joined
28 Jun 2005
Posts
997
Location
London
Hello

I'm a bit stuck, I'm trying to have a template that will look at a variable and know which array to look at.

Say I had 2 arrays german & french.

var german = new Array();
german[0] = "German1";
german[1] = "German2";

var french= new Array();
german[0] = "German1";
german[1] = "German2";

I would like to be able to have a variable "market".

So if I set:
var market="german";

And put this in the HTML:

document.write(market[0]);

I would like it to display "German1"

Instead it is treating var market as a string and displaying the first element of the string "G".

I'd appreciate any help on this!

Thanks!
 
Code:
var market = german;
document.write(market[0]);

Lose the quotes ;). With quotes you are saying that market = the string german, rather than copying whatever is in the german variable (the array of data) to it.
 
Great, that worked and it's awesome BUT..

If I declare that first and then the array I get an undefined error if I put alert(market);

I assume that this is because the variable is the same as the array.

Is there anyway I could have the original variable as german1 then after declaring the array remove the 1 from the variable so it's back to german - thus it working!

Thanks!
 
Heh yeah, the statement needs the array it's copying the data from to be declared first - otherwise you're saying that var market = <null> which just doesn't work.
 
Back
Top Bottom