Link to home
Start Free TrialLog in
Avatar of Schmeck
Schmeck

asked on

Calling a specific Javascript array

This is probably an easy questions, but I'm a bit of a JavaScript newbie,

I have created and filled a bunch of arrays using an ASP loop. The arrays were created looking like this
var amount1 = new Array(250, 0)
var amount2 = new Array(500, 0)
var amount3 = new Array(0, 250)
...and so on

So, I want to call a specific array, based on a variable passed into the function (intSel). This variable will be used to select the correct array by providing the number after "amount". How do I concatenate this thing to return the values contained in the array itself, instead of just a string.

Right now I have a line that says:
document.getElementById('Debit0').value = "amount" + intSel + "[0]";

But of course that doesn't work and returns "amount2[0]", for example.
I want it to return 500.

Thanks for your help!
Dan
ASKER CERTIFIED SOLUTION
Avatar of Zontar
Zontar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zontar
Zontar

Also, instead of

 document.getElementById('Debit0').value = eval("amount" + intSel + "[0]");

you could use

 document.getElementById('Debit0').value = window["amount" + intSel][0];

prodvided that amount1, amount2, etc. are defined as global variables and not as local to a function.
Avatar of Schmeck

ASKER

Thanks Zontar...that's what I was looking for!
Cool. No worries, Schmeck.

Technically, JavaScript doesn't support multi-dimensional arrays, but array elements can be of any type -- including arrays -- which makes that sort of a moot point. ;)

(Arrays of objects/associative arrays can also be handy at times when you need to roll your own data structures in a hurry.)