Created
February 16, 2016 00:07
-
-
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.
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
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