Skip to main content
added 165 characters in body
Source Link
user2864740
  • 62.5k
  • 15
  • 159
  • 234

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

On object literals, the key is always interpreted literally, as a string. To use a "dynamic" key, you have to use bracket notation:

var obj = {}; obj[myKey] = value; 

In your case:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ var key = this.attr('name') , value = this.attr('value') , ret = {}; ret[key] = value;   return ret; }) callback(null, inputs); } 

 

Edit: In the new ES2015 standard for JavaScript (formerly called ES6), objects can be created with computed keys: Object Initializer spec. The syntax is:

var obj = {   [myKey]: value, } 

If applied to the OP's scenario, it would turn into:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){   return {  [this.attr('name')]: this.attr('value'), }; }) callback(null, inputs); } 

Using Babel or Google's traceur, it is possible to try this syntax today

In the new ES2015 standard for JavaScript (formerly called ES6), objects can be created with computed keys: Object Initializer spec.

The syntax is:

var obj = { [myKey]: value, } 

If applied to the OP's scenario, it would turn into:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ return {   [this.attr('name')]: this.attr('value'),   }; }) callback(null, inputs); } 

Note: 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 (ES5 and below), the 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] = value; 

In your case:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ var key = this.attr('name')  , value = this.attr('value')   , ret = {};    ret[key] = value;  return ret; }) callback(null, inputs); } 
ES6 has been published
Source Link
Moshe Katz
  • 16.5k
  • 7
  • 71
  • 127

On object literals, the key is always interpreted literally, as a string. To use a "dynamic" key, you have to use bracket notation:

var obj = {}; obj[myKey] = value; 

In your case:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ var key = this.attr('name') , value = this.attr('value') , ret = {}; ret[key] = value; return ret; }) callback(null, inputs); } 

Edit: For ES6In the new (the next version of Javascript), there's a proposalES2015 standard for JavaScript (as of Nov. 2013formerly called ES6) for a syntax to create, objects can be created with computed keyscomputed keys: Object Literal Computed Property KeysObject Initializer spec. The syntax is:

var obj = { [myKey]: value, } 

If applied to the OP's scenario, it would turn into:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ return { [this.attr('name')]: this.attr('value'), }; }) callback(null, inputs); } 

Using Babel or Google's traceur, it is possible to try this syntax today

On object literals, the key is always interpreted literally, as a string. To use a "dynamic" key, you have to use bracket notation:

var obj = {}; obj[myKey] = value; 

In your case:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ var key = this.attr('name') , value = this.attr('value') , ret = {}; ret[key] = value; return ret; }) callback(null, inputs); } 

Edit: For ES6 (the next version of Javascript), there's a proposal (as of Nov. 2013) for a syntax to create objects with computed keys: Object Literal Computed Property Keys. The syntax is:

var obj = { [myKey]: value, } 

If applied to the OP's scenario, it would turn into:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ return { [this.attr('name')]: this.attr('value'), }; }) callback(null, inputs); } 

Using Babel or Google's traceur, it is possible to try this syntax today

On object literals, the key is always interpreted literally, as a string. To use a "dynamic" key, you have to use bracket notation:

var obj = {}; obj[myKey] = value; 

In your case:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ var key = this.attr('name') , value = this.attr('value') , ret = {}; ret[key] = value; return ret; }) callback(null, inputs); } 

Edit: In the new ES2015 standard for JavaScript (formerly called ES6), objects can be created with computed keys: Object Initializer spec. The syntax is:

var obj = { [myKey]: value, } 

If applied to the OP's scenario, it would turn into:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ return { [this.attr('name')]: this.attr('value'), }; }) callback(null, inputs); } 

Using Babel or Google's traceur, it is possible to try this syntax today

Mention babel
Source Link
Renato Zannon
  • 30.2k
  • 7
  • 42
  • 47

On object literals, the key is always interpreted literally, as a string. To use a "dynamic" key, you have to use bracket notation:

var obj = {}; obj[myKey] = value; 

In your case:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ var key = this.attr('name') , value = this.attr('value') , ret = {}; ret[key] = value; return ret; }) callback(null, inputs); } 

Edit: For ES6 (the next version of Javascript), there's a proposal (as of Nov. 2013) for a syntax to create objects with computed keys: Object Literal Computed Property Keys. The syntax is:

var obj = { [myKey]: value, } 

If applied to the OP's scenario, it would turn into:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ return { [this.attr('name')]: this.attr('value'), }; }) callback(null, inputs); } 

Using Babel or Google's traceur, it is possible to try this syntax today

On object literals, the key is always interpreted literally, as a string. To use a "dynamic" key, you have to use bracket notation:

var obj = {}; obj[myKey] = value; 

In your case:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ var key = this.attr('name') , value = this.attr('value') , ret = {}; ret[key] = value; return ret; }) callback(null, inputs); } 

Edit: For ES6 (the next version of Javascript), there's a proposal (as of Nov. 2013) for a syntax to create objects with computed keys: Object Literal Computed Property Keys. The syntax is:

var obj = { [myKey]: value, } 

If applied to the OP's scenario, it would turn into:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ return { [this.attr('name')]: this.attr('value'), }; }) callback(null, inputs); } 

Using Google's traceur, it is possible to try this syntax today

On object literals, the key is always interpreted literally, as a string. To use a "dynamic" key, you have to use bracket notation:

var obj = {}; obj[myKey] = value; 

In your case:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ var key = this.attr('name') , value = this.attr('value') , ret = {}; ret[key] = value; return ret; }) callback(null, inputs); } 

Edit: For ES6 (the next version of Javascript), there's a proposal (as of Nov. 2013) for a syntax to create objects with computed keys: Object Literal Computed Property Keys. The syntax is:

var obj = { [myKey]: value, } 

If applied to the OP's scenario, it would turn into:

stuff = function (thing, callback) { var inputs = $('div.quantity > input').map(function(){ return { [this.attr('name')]: this.attr('value'), }; }) callback(null, inputs); } 

Using Babel or Google's traceur, it is possible to try this syntax today

named and added a link to the "other syntax"
Source Link
Felix Kling
  • 820.1k
  • 181
  • 1.1k
  • 1.2k
Loading
add traceur links
Source Link
Renato Zannon
  • 30.2k
  • 7
  • 42
  • 47
Loading
Add edit for the ES6 syntax
Source Link
Renato Zannon
  • 30.2k
  • 7
  • 42
  • 47
Loading
Source Link
Renato Zannon
  • 30.2k
  • 7
  • 42
  • 47
Loading