Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object from comprehension in CoffeeScript [dict/hash comprehensions]

Tags:

coffeescript

is there a way to return an object from a comprehension in coffeescript? something so that i could express this:

form_values = () ->   ret = {}   ret[f.name] = f.value for f in $('input, textarea, select')   return ret 

like this:

form_values = () -> f.name, f.value for f in $('input, textarea, select') 

i'd like to construct a single object (not an array of objects). so if the markup looks something like this:

<form name=blah>   <input type=text name=blah1 value=111 />   <textarea name=blah2>222</textarea>   <select name=blah3>     <option value=333a>     <option value=333b>   </select> </form> 

the returned object would be something like this:

{   blah1: '111',   blah2: '222',   blah3: '' } 
like image 557
aaronstacy Avatar asked Oct 31 '11 02:10

aaronstacy


2 Answers

form_values = new ->   @[f.name] = f.value for f in $ 'input, textarea, select'   this 

or

form_values = new class then constructor: ->   @[f.name] = f.value for f in $ 'input, textarea, select' 
like image 194
matyr Avatar answered Sep 18 '22 05:09

matyr


Nope. Comprehensions only return arrays in CoffeeScript. Search the issue tracker for object comprehensions, and you'll find several proposals, but none were found suitable.

like image 45
Trevor Burnham Avatar answered Sep 20 '22 05:09

Trevor Burnham