Solved

Calling a specific Javascript array

Posted on 2003-11-07
4
258 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now