开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜