September 2017
Intermediate to advanced
216 pages
6h 8m
English
Basic arithmetic is another use of reducers. In the preceding section, we looked at minimums and maximums—the reduction is produced by performing comparisons. With accumulators, we're just changing the reduction based on simple addition and other operators. Let's take a look at some basic accumulators:
const myList = List.of( Map.of('red', 3, 'black', 4), Map.of('red', 6, 'black', 8), Map.of('red', 9, 'black', 12));const redSum = myList.reduce( (result, v) => result + v.get('red'), 0);const blackSum = myList.reduce( (result, v) => result + v.get('black'), 0);console.log('myList', myList.toJS());// -> myList [ { red: 3, black: 4 },// -> { red: 6, black: 8 },// -> { red: 9, black: 12 } ]console.log('redSum', redSum);// -> ...Read now
Unlock full access