jwqrwq
Last Updated: February 25, 2016
·
3.34K
· poupougnac
1505228 10153667646265066 1410509922 n

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'
Say Thanks
Respond