Beautiful Map And Looping in CoffeeScript

Submitted by swarut on Mon, 07/09/2012 - 20:17

I wrote

  1. ObjectsFromArray = (array_object) ->  
  2.   objects = []
  3.   for text in array_object
  4.    do (text) ->
  5.      item = {}
  6.      item.name = text
  7.      objects.push(item)
  8.   objects
But it is better to write this way (by the senior in doi project)
  1. tagObjectsFromTextValue = (tagsString) ->
  2.     return [] unless tagsString.length
  3.     tagArray = $.map(tagsString.split(','), (tag) -> $.trim(tag))
  4.     { name: tag } for tag in tagArray