Skip to content

Instantly share code, notes, and snippets.

@JonMidhir
Last active December 16, 2015 02:28
Show Gist options
  • Save JonMidhir/5362235 to your computer and use it in GitHub Desktop.
Save JonMidhir/5362235 to your computer and use it in GitHub Desktop.
Array iteration fun in CoffeeScript
# 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