Last active
December 16, 2015 02:28
-
-
Save JonMidhir/5362235 to your computer and use it in GitHub Desktop.
Array iteration fun in CoffeeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Say you want to iterate over a variable that might be an array or null/undefined, | |
# you can do this: | |
for order in orders or [] | |
# Do something with orders | |
# This simply iterates over the empty array if book is null or undefined. Handy if | |
# you have an object like this: | |
customers = { | |
1934: [{Order ...}, {Order ...}, {Order ...}], | |
1942: null, | |
1963: [{Order ...}] | |
} | |
# And you want to iterate over it to show orders per customer: | |
for customer, customer_orders of customers | |
order.checked = true for order in customer_orders or [] | |
# Pretty cool. Although it'd be preferable to have nulls cast to Arrays in the | |
# customers object to begin with! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment