0

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?

4 Answers 4

2

process can access alpha and bravo directly (without being passed).

The reason why you can't access those variable via window[name] is because they are not global variables.

function demo2()
{
    var alpha = [];
    alpha["a"] = "test1";
    var bravo = [];
    bravo["a"] = "test2"; 

    function process(name)
    {
        alert(alpha["a"]);
        alert(bravo["a"]);
    }
    process();
}

Also, using alphanumeric array subscripts kinda beats the purpose of using array (not that it's not possible). The elements with non-numeric subscripts are left out of all the built-in array functions (such as join, slice, shift).

You can use plain objects for that purpose.

var obj = {};
obj.a = "test";   //OR
obj["a"] = "test"; 

[Edit]: Responding to your comment, there's almost never a good reason to refer to variables with a string containing their name. But if you have to do it, you may consider making all your arrays properties of an object. This is same as referring to window["something"] if they were global variables except you're substitution window with your own object.

function demo2()
{
    var container = {};
    container.alpha = [];
    container.alpha["a"] = "test1";
    container.bravo = [];
    container.bravo["a"] = "test2"; 

    function process(name)
    {
        alert(container[name]["a"]);
    }
    process("alpha");
    process("bravo");
}
Sign up to request clarification or add additional context in comments.

1 Comment

I know we can access the array directly. I have just changed the question. Hope you understand what I want.
2

What about this?

function demo2()
{
    var alpha = [];
    alpha["a"] = "test1";
    var bravo = [];
    bravo["a"] = "test2"; 

    function process(name)
    {
        alert(name["a"]);
    }
    process(alpha);
    process(bravo);
}

Comments

0
function demo2()
{
var alpha = [];
alpha["a"] = "test1";
var bravo = [];
bravo["a"] = "test2"; 

function process(name)
{
        var x = eval(name);
        alert(x["a"]);
}
process("alpha");
process("bravo");

}

3 Comments

Topicstarter's cases should be avoided ) Don't understand why not use local variables in closure.
What I want is like this case. 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.
Sorry, my fault - omit your comment about eval. But I don't know another way to make variable from string (only global).
0

To answer your question:

function process(name) {
        alert(eval(name+'["a"]'));
}

Disclaimer: If you don't have a good reason to do this, I'd refactor your scoping - it's pretty ugly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.