Share your code. Components are building blocks, you are the architect.

utils/reduce-by
v1.1.0
1.1.0
1.0.0
Groups the elements of the list according to the result of calling

Please configure Bit as a scoped registry to install components with NPM.Learn more

npm config set '@bit:registry' https://node.bitsrc.io
Use utils/reduce-by in your project
npm i @bit/bit.ramda.utils.reduce-by
Copied

reduceBy (valueFn:Function, acc:*, keyFn:Function, list:Array) : Object

Groups the elements of the list according to the result of calling the String-returning function keyFn on each element and reduces the elements of each group to a single value via the reducer function valueFn.

This function is basically a more general groupBy function.

Acts as a transducer if a transformer is given in list position.

Example

var reduceToNamesBy = R.reduceBy((acc, student) => acc.concat(student.name), []);
     var namesByGrade = reduceToNamesBy(function(student) {
       var score = student.score;
       return score < 65 ? 'F' :
              score < 70 ? 'D' :
              score < 80 ? 'C' :
              score < 90 ? 'B' : 'A';
     });
     var students = [{name: 'Lucy', score: 92},
                     {name: 'Drew', score: 85},
                     // ...
                     {name: 'Bart', score: 62}];
     namesByGrade(students);
     // {
     //   'A': ['Lucy'],
     //   'B': ['Drew']
     //   // ...,
     //   'F': ['Bart']
     // }

Arguments

valueFn: Function

The function that reduces the elements of each group to a single value. Receives two values, accumulator for a particular group and the current element.

acc: *

The (initial) accumulator value for each group.

keyFn: Function

The function that maps the list's element into a key.

list: Array

The array to group.

Returns

Object

An object with the output of `keyFn` for keys, mapped to the output of `valueFn` for elements which produced that key when passed to `keyFn`.