If only string is allowed to pass into function process(), what should we do inside the function process() to access the array value. Eval is one of the method, but its usage is not suggested by many people.
function demo2(name2)
{
var alpha = [];
alpha["a"] = "test1";
var bravo = [];
bravo["a"] = "test2";
function process(name)
{
alert(window[name]["a"]);
}
process(name2); // error
}
name2 can be "alpha" or "bravo" or many other arrays' name.
var alpha = [];
alpha["a"] = "test1";
var bravo = [];
bravo["a"] = "test2";
function process(name)
{
alert(window[name]["a"]);
}
process("alpha");
For the second example, it works fine. I just want to pass the string into the function and use it as the array name in the first example. I have the 2nd example and so I would like to know how I can do this inside a function.
What should I write inside the function process to alert the alpha and bravo variables?