How can I turn a string into a key? [duplicate]
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);
精彩评论