group-by

v0.9.6arrow_drop_down
v0.9.6
STATUS
Passing
DOWNLOADS
2
VISIBILITY
Public
PUBLISHED
5 years ago
SIZE
2 KB
Splits a list into sub-lists stored in an object, based on the result of
1 contributor
Install group-by as a package?
Copied
npm i @bit/justin-capalbo.ramda.group-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
groupBy (
fn:Function,
list:Array
) : Object

Splits a list into sub-lists stored in an object, based on the result of calling a String-returning function on each element, and grouping the results according to values returned.

Dispatches to the groupBy method of the second argument, if present.

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

Example
const byGrade = R.groupBy(function(student) {
       const score = student.score;
       return score < 65 ? 'F' :
              score < 70 ? 'D' :
              score < 80 ? 'C' :
              score < 90 ? 'B' : 'A';
     });
     const students = [{name: 'Abby', score: 84},
                     {name: 'Eddy', score: 58},
                     // ...
                     {name: 'Jack', score: 69}];
     byGrade(students);
     // {
     //   'A': [{name: 'Dianne', score: 99}],
     //   'B': [{name: 'Abby', score: 84}]
     //   // ...,
     //   'F': [{name: 'Eddy', score: 58}]
     // }
Arguments
fn: Function

Function :: a -> String

list: Array

The array to group

Returns
Object

An object with the output of `fn` for keys, mapped to arrays of elements that produced that key when passed to `fn`.

Help and resources