On object literals,In the key is always interpreted literally, as a string. To use a "dynamic" keynew ES2015 standard for JavaScript (formerly called ES6), you have to useobjects can be created with bracket notationcomputed keys: Object Initializer spec.
The syntax is:
var obj = {}; obj[myKey] = value;[myKey]: value, } In your caseIf applied to the OP's scenario, it would turn into:
stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ varreturn key{ = this [this.attr('name') , value =]: this.attr('value') , ret = {}; ret[key] = value; return ret; }) callback(null, inputs); } EditNote:: In the new ES2015 standard A transpiler is still required for browser compatiblity.
Using Babel or Google's traceur, it is possible to use this syntax today.
In earlier JavaScript specifications (formerly called ES6ES5 and below), objects can be created with computed keys: Object Initializer spec. The syntaxthe key in an object literal is always interpreted literally, as a string.
To use a "dynamic" key, you have to use bracket notation:
var obj = {}; obj[myKey] [myKey]:= value, }value; If applied to the OP's scenario, it would turn intoIn your case:
stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ return { var key = [thisthis.attr('name')]: , value = this.attr('value'), , ret = {}; ret[key] = value; return ret; }) callback(null, inputs); } Using Babel or Google's traceur, it is possible to try this syntax today