reduce-by

v0.9.6arrow_drop_down
v0.9.6
STATUS
Passing
DOWNLOADS
5
VISIBILITY
Public
PUBLISHED
5 years ago
SIZE
2 KB
Groups the elements of the list according to the result of calling
1 contributor
Install reduce-by as a package?
Copied
npm i @bit/justin-capalbo.ramda.reduce-by
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.cloud
Component Example
React
React
Vue
Angular
React Native
Add dependency... help_outline
Just
import
any of the 1 million components
and packages in Bit or NPM to the example.
import Button from '@bit/grommet.grommet.button';
import Lodash from 'lodash';
toggle layout
No preview available
modifieddraft
chevron_left
chevron_right
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
const groupNames = (acc, {name}) => acc.concat(name)
     const toGrade = ({score}) =>
       score < 65 ? 'F' :
       score < 70 ? 'D' :
       score < 80 ? 'C' :
       score < 90 ? 'B' : 'A'

     var students = [
       {name: 'Abby', score: 83},
       {name: 'Bart', score: 62},
       {name: 'Curt', score: 88},
       {name: 'Dora', score: 92},
     ]

     reduceBy(groupNames, [], toGrade, students)
     //=> {"A": ["Dora"], "B": ["Abby", "Curt"], "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`.

Help and resources