Skip to main content
Module

x/ramda/groupBy.js

:ram: Practical functional Javascript
Very Popular
Go to Latest
File
import _checkForMethod from './internal/_checkForMethod';import _curry2 from './internal/_curry2';import reduceBy from './reduceBy';
/** * 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. * * @func * @memberOf R * @since v0.1.0 * @category List * @sig (a -> String) -> [a] -> {String: [a]} * @param {Function} fn Function :: a -> String * @param {Array} list The array to group * @return {Object} An object with the output of `fn` for keys, mapped to arrays of elements * that produced that key when passed to `fn`. * @see R.reduceBy, R.transduce * @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}] * // } */var groupBy = _curry2(_checkForMethod('groupBy', reduceBy(function(acc, item) { if (acc == null) { acc = []; } acc.push(item); return acc;}, null)));export default groupBy;