CoffeeScript : iterate anywhere (for...in, for...of) !
Here is a small summary of some useful iterations in Coffeescript
Arrays
Associative array :
ages = {}
ages["jim"] = 12
ages["john"] = 7
for key, value of ages
console.log key + " is " + value
Indexed array :
arr = ["a","b","c"]
for value in arr
console.log "value without the key is :" + value
for value, key in arr
console.log key + " is " + value
Objects
yearsOld = john: 10, bob: 9, lisa: 11
for child, age of yearsOld
console.log child + ' has ' + age + ' year old'
To avoid properties inherited by the prototype :
for own child, age of yearsOld
console.log child + ' has ' + age + ' year old'
Written by pmaoui
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Coffeescript
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#