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);

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?

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


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

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]; 


will this do what you are aiming for?

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.

it takes years of experience to write simple code.

So does it solve your problem or not?!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.