Poru
Poru

Reputation: 8360

JavaScript: Custom property with vars

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

Answers (2)

Boaz Yaniv
Boaz Yaniv

Reputation: 6424

What about this?

function someCoolActionHere(input) {
    obj = {};
    obj[input] = 'someValue';
    return obj;
}

Upvotes: 1

Matti Virkkunen
Matti Virkkunen

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

Related Questions