0

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"});
Community
  • 1
  • 1
Eugene
  • 10,957
  • 20
  • 69
  • 97
  • 3
    Uhm... exactly the same way. It's the same question as far as I can see. It has nothing to do with functions, just how to create the object literal properly. You cannot use variables as keys in object literals, hence you cannot use it this way. You have to create the object first and then pass it to the function. – Felix Kling Dec 31 '12 at 15:40

1 Answers1

4

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();
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636