Solved

Calling a specific Javascript array

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Standards for file storage. 3 19
Create 4 <div> below each other in php 2 58
 shows up in Outlook, not OWA or on phone 3 62
Problem to Popup 2 76
Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

920 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

12 Experts available now in Live!

Get 1:1 Help Now