Reputation: 8360
I have a JavaScript function like that:
function someCoolActionHere(input) {
return { input: 'someValue' };
}
This function returns an JS object. The function parameter input
is a string (e.g. name
) and the value of the paramter variable should be used as property name in the object, not input
itself. Example:
someCoolActionHere('hello');
// => { 'hello': 'someValue' }
How could that be done?
Upvotes: 1
Views: 134
Reputation: 6424
What about this?
function someCoolActionHere(input) {
obj = {};
obj[input] = 'someValue';
return obj;
}
Upvotes: 1
Reputation: 65156
Can't do it with a literal. You'll have to use []
to set the property.
var obj = {};
obj[input] = "someValue";
return obj;
Upvotes: 2