groupBy.js 387 B

1234567891011121314151617
  1. export function groupBy(values, predicate) {
  2. return values.reduce(function (acc, item) {
  3. var key = predicate(item);
  4. if (!acc.hasOwnProperty(key)) {
  5. acc[key] = [];
  6. } // We limit each section to show 5 hits maximum.
  7. // This acts as a frontend alternative to `distinct`.
  8. if (acc[key].length < 5) {
  9. acc[key].push(item);
  10. }
  11. return acc;
  12. }, {});
  13. }