0

My project requires that I have several arrays defined in javascript such as:

var d1 = new Array(2,17,18,19,20);
var d2 = new Array(2,18,19,20);
var d3 = new Array(2,18,21,19,20);
var d4 = new Array(2);
var d5 = new Array(2,18,19,20);
var d6 = new Array(2,18,21,19,20);
etc

I have a <select> element which contains options with integer values (1,2,3,4...). When the value of said <select> is changed I need to be able to dynamically switch to the corresponding array.

What is the best way of achieving this?

Edit:
I forgot about the eval function, so I added something like this to test it:

alert(eval('d'+value)[0]);

However, the problem now is that arrays like d4, with only one value in them are showing up as undefined.

Edited by MysticalNomad: Update on code...

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by Troy III
0

How about:

var d=[];

    d[1] = [2,17,18,19,20];
    d[2] = [2,18,19,20];
    d[3] = [2,18,21,19,20];
    d[4] = [2];
    d[5] = [2,18,19,20];
    d[6] = [2,18,21,19,20]; 
    //etc..

alert(d[value][0])

will this do what you are aiming for?

Edited by Troy III

0

An array of arrays... I actually hadn't considered that! Thank you.

It's true that the longer you look at a problem the more likely the simplest answer is to be overlooked.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.