adhemo
adhemo

Reputation: 3

find a return value as variable

I have dynamic variable called "id_item" which returns different values depending on the words found in the URL of the document. So, for example, if the URL contains "?item=i1990", var "id_item" will be "i1990".

In the same JS I have declared several variable arrays to store some specific data. Those variables are expected to be the same as the words found in the URL. For the same example, there is a variable array called i1990 with some data.

var i1990 = ["valueexample1", "valueexample2", "valueexample3"];

What I need is to get first the value of "id_item" and then search for it as a variable and get its value... to arrive until "valueexample1", "valueexample2" or "valueexample3" depending of my needs (it's an array)

I don't know if I've been clear, but I thank you all for your support.

Upvotes: 0

Views: 51

Answers (2)

Grainier
Grainier

Reputation: 1654

Instead of having several variables for arrays of data. Have a map of data with item ids as keys.

var id_item = 'i1990'; // assume your id is i1990

var items = {
    'i1990' : ["valueexample1", "valueexample2", "valueexample3"],
    'i1991' : ["valueexample1", "valueexample2", "valueexample3"],
    'i1992' : ["valueexample1", "valueexample2", "valueexample3"]
}

var item = items[id_item];
console.log(item);

Upvotes: 1

Cortwave
Cortwave

Reputation: 4907

You can use JavaScript dictionary for this issue.

var dictionary = {};
dictionary["one"] = array1;
dictionary["two"] = array2;
dictionary["three"] = array3;

Upvotes: 0

Related Questions