4-1-arrays.coffee 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. storeLocations1 = [
  2. 'Orlando'
  3. 'Winter Park'
  4. 'Sanford'
  5. ]
  6. # Beware, this doesn't compile correctly:
  7. # storeLocations.forEach(location, index) ->
  8. # It uses it as a forEach(location, index) call with a () => {} callback
  9. # So be sure to add the space.
  10. storeLocations1.forEach (location, index) ->
  11. console.log "Location #{index}: #{location}"
  12. # Or use something more idiomatic:
  13. for location, index in storeLocations1
  14. console.log "Location #{index}: #{location}"
  15. # List comprehensions
  16. console.log "Location #{index}: #{location}" for location, index in storeLocations1
  17. # In this case, the parentheses are essential
  18. storeLocations2 = ("#{loc}, FL" for loc in storeLocations1)
  19. # Without them, the whole "storeLocations = ..." part is run for each loc.
  20. console.log storeLocations2
  21. geoLocate = (loc) ->
  22. return "#{loc}, USA"
  23. # Applying a filter to a list comprehension
  24. console.log (geoLocate(loc) for loc in storeLocations1 when loc isnt 'Sanford')
  25. storeLocations3 = (loc for loc in storeLocations1 when loc isnt 'Sanford')
  26. console.log(storeLocations3)