开发者

How can I turn a string into a key? [duplicate]

开发者 https://www.devze.com 2023-04-01 07:48 出处:网络
This question already has answers here: Javascript create variable from its name (5 answers) Closed 8 years ago.
This question already has answers here: Javascript create variable from its name (5 answers) Closed 8 years ago.

I need to run this line of Javascript:

@model.save({ name: @some_element.val() })

But the key, which in this case 开发者_开发百科is name, will change depending on the value of a variable. The variable is a string representation of the key. So in this case, the variable is "name". How can I use the variable to specify the correct key? If I use the variable name directly it is interpreted as the key itself.


var obj = {};
obj[varName] = @some_element.val();
@model.save(obj);
0

精彩评论

暂无评论...
验证码 换一张
取 消