Solved

Calling a specific Javascript array

Posted on 2003-11-07
4
260 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:Schmeck
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
Zontar earned 200 total points
ID: 9706010
document.getElementById('Debit0').value = eval("amount" + intSel + "[0]");

A better way to handle these might be as a single array:

var amount = [ [250, 0], [500, 0], [0, 250] ];

Each element in amounts is itself an array with two elements;

amount[0][0] = 250... amount[0][1] = 0... amount[1][0] = 500... and so on. In this case you could use

document.getElementById('Debit0').value = amount[intSel][0];
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9706024
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.
0
 

Author Comment

by:Schmeck
ID: 9706281
Thanks Zontar...that's what I was looking for!
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9708256
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.)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question