?
Solved

Calling a specific Javascript array

Posted on 2003-11-07
4
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
Zontar earned 800 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

801 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