Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a parameter passed to a function as a variable name

Tags:

javascript

I'm a JS noob, so apologies if I've missed the obvious but the searches I did failed to retrieve anything that made sense to me.

I've been trying to use a parameter passed to a function, which makes an ajax call, as the name for the variable for where the data should go. For example:

ajaxFun("[url loctaion].json?callback=?","main","mainData");

The idea is that the retrieved JSON object is then accessible using the 'mainData' variable, however I can't use this explicitly as I have another file to retrieve which should be made accessible under the variable name 'expData'. Both of these are global variables and are already declared when the page loads.

Below is the current code which does work, but I would like to abstract away the 'if, else if, else' block in the success portion of the ajax call:

    function ajaxFun(urlS,jsonpCallbackS,dataSource){
    $.ajax({
            type: "GET",
            url: urlS,
            async: false,
            jsonpCallback: jsonpCallbackS,
            contentType: "application/json",
            dataType: "jsonp",
            cache: false,
            success: function(source){
            if(dataSource === "expData"){
                expData = source;
            }
            else if(dataSource === "mainData"){
                mainData = source;
            }
            else{
                console.log('data not set');
            }
            },
            error: function(classData){
                console.log('error');
            }
    });
}

I've tried various methods including using eval() and appending the result to an object:

var dataCon = {};

function ajaxFun(urlS,jsonpCallbackS,dataSource){
    $.ajax({
            type: "GET",
            url: urlS,
            jsonpCallback: jsonpCallbackS,
            contentType: "application/json",
            dataType: "jsonp",
            cache: false,
            success: function(source){
                var name = dataSource;
                alert(name);
                dataCon.name = source;
                },
                error: function(classData){
                    console.log('error');
                }
    });
}

Using:

'ajaxFun("[url loctaion].json?callback=?","main","mainData");'

I get the alert 'mainData' but in the console.log for dataCon I can see that the value of 'source' has been put to the key called 'name'.

like image 898
joesch Avatar asked Dec 07 '25 00:12

joesch


2 Answers

Try this:

window[dataSource]

That will get the value of the variable in the string you have. For example:

dataSource = "something"
something = 5
alert(window[dataSource]) // alerts 5
like image 74
tckmn Avatar answered Dec 08 '25 13:12

tckmn


Use square-bracket notation:

dataCon[name]

or

dataCon[source]

See the MDN documentation on accessing object properties

like image 38
Michael Paulukonis Avatar answered Dec 08 '25 14:12

Michael Paulukonis