Created
July 8, 2017 19:13
-
-
Save dmjcomdem/980ddf902e4af089b1e2729e969e27f3 to your computer and use it in GitHub Desktop.
advanced-reduce
This file contains hidden or 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
| let selected = [ | |
| { price: 20 }, | |
| { price: 45 }, | |
| { price: 67 }, | |
| { price: 1305 } | |
| ]; | |
| let reducers = { | |
| rubles : (state, item) => { return state.rubles += item.price }, | |
| dollars: (state, item) => { return state.dollars += item.price / 71.6024 }, | |
| euros : (state, item) => { return state.euros += item.price / 79.0133 }, | |
| yens : (state, item) => { return state.yens += item.price / 0.6341 }, | |
| pounds : (state, item) => { return state.pounds += item.price / 101.7829 } | |
| }; | |
| let combineReducers = reducers => { | |
| return (state, item) => { | |
| return Object.keys(reducers).reduce( (nextState, key) => { | |
| reducers[key](state, item); | |
| return state; | |
| }, {}); | |
| } | |
| }; | |
| let priceReducer = combineReducers(reducers); | |
| let totalPrice = selected.reduce(priceReducer, { | |
| rubles: 0, | |
| pounds: 0, | |
| dollars: 0, | |
| euros: 0, | |
| yens: 0 | |
| }); | |
| console.log(totalPrice); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment