Skip to content

Instantly share code, notes, and snippets.

@bendrucker
Created February 16, 2016 00:07
Show Gist options
  • Save bendrucker/c7510a0c7872362e2f17 to your computer and use it in GitHub Desktop.
Save bendrucker/c7510a0c7872362e2f17 to your computer and use it in GitHub Desktop.
Using reducers to avoid hard-to-read side effects. This is probably a bad idea unless your list is really long and rarely changes. You should probably just loop multiple times and then cache the result efficiently using an immutable store.
return orders.reduce(function (accumlator, order) {
accumulator.aggregates.count++
accumulator.aggregates.total += order.total
accumulator.orders.push(order)
return accumulator
}, OrderStore())
function OrderStore () {
return {
aggregates: {
total: 0,
count: 0
},
orders: []
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment