Question

Possible Duplicate:
Use key’s value as key in key-value pair in Javascript

As a followup to Use key's value as key in key-value pair in Javascript function parameter. How can you use the value of a key-value pair as the key in a different key-value pair in a Javascript function param?

I'd like to do the following:

var params = {param1: "paramname1"};
somefunction({params.param1:"param1value"});

So that things essentially equals:

somefunction({"paramname1":"param1value"});
Was it helpful?

Solution

To use an expression as a property name you need to use the [] syntax instead of an object literal:

var params = {
    param1: "paramname1"
};
var data = {};
data[params.param1] = "param1value";
somefunction();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top