可能重复:
Use key’s value as key in key-value pair in Javascript
作为Use key's value as key in key-value pair in Javascript函数参数的后续内容。如何使用键值对的值作为Javascript函数参数中不同键值对的键?
我想做以下事情:
var params = {param1: "paramname1"};
somefunction({params.param1:"param1value"});
所以事情基本上等于:
somefunction({"paramname1":"param1value"});
答案 0 :(得分:4)
要将表达式用作属性名称,您需要使用[]
语法而不是对象文字:
var params = {
param1: "paramname1"
};
var data = {};
data[params.param1] = "param1value";
somefunction();