Ramda.js R.collectBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.collectBy(fn, list) calls fn on every item to get a key, then puts items with the same key into the same sub-list (official Ramda docs). The result is an array of groups — the keys themselves are not returned.

Stable since v0.28.0: R.collectBy is not deprecated, experimental, or non-standard, but older Ramda versions don’t include it. It returns only the groups; use R.groupBy when you also need the keys.

Syntax

Call R.collectBy. Start with this line.

Ramda
R.collectBy(fn, list) or R.collectBy(fn)(list)
CallWhat it does
R.collectBy(fn, list) or R.collectBy(fn)(list)Primary signature.
R.collectBy(...)Call the list helper.

Prefer import { collectBy } from "ramda" when you want a named import.

Library declarations
Ramda
R.collectBy(fn, list) or R.collectBy(fn)(list)

Parameters

ParameterDescription
fnRequired. Returns the group key for one item
listOptional. The array to split; omit it to get a reusable function

Return Value

Return ValueDescription
*A new array of arrays; the input is never changed

Ramda Examples

Example 1 — Official Meals by Type

Group five meals by their type — the sample from the Ramda docs.

Ramda
const meals = [
  { type: 'breakfast', item: 'coffee' },
  { type: 'lunch', item: 'burrito' },
  { type: 'dinner', item: 'pasta' },
  { type: 'breakfast', item: 'croissant' },
  { type: 'lunch', item: 'pizza' }
];

const groups = R.collectBy(R.prop('type'), meals);

groups.forEach((group) => console.log(group));
Try It Yourself

How It Works

  1. Key. R.prop('type') reads each meal’s type: breakfast, lunch, dinner, breakfast, lunch.
  2. Bucket. Meals with the same type go into the same sub-list.
  3. Order. Breakfast comes first because it was seen first; inside each group, meals keep their list order.
  4. Result. Three groups come back as an array; the words “breakfast”, “lunch”, and “dinner” are not included.

Example 2 — Split Numbers into Odd and Even

Your own key function works just as well as a Ramda helper.

Ramda
const parity = (n) => (n % 2 === 0 ? 'even' : 'odd');

console.log(R.collectBy(parity, [1, 2, 3, 4, 5]));
// [[1, 3, 5], [2, 4]]
Try It Yourself

How It Works

  1. Key. parity labels each number 'odd' or 'even'.
  2. Bucket. 1, 3, 5 share 'odd'; 2, 4 share 'even'.
  3. Order. The odd group is first because 1 (odd) is the first item.
  4. Text keys. Returning words like 'odd' keeps the first-seen order predictable.

Example 3 — Group Words by First Letter

Use R.head as the key function to bucket words alphabetically.

Ramda
const fruits = ['apple', 'banana', 'avocado', 'blueberry', 'cherry'];

console.log(R.collectBy(R.head, fruits));
// [['apple', 'avocado'], ['banana', 'blueberry'], ['cherry']]
Try It Yourself

How It Works

  1. Key. R.head on a string returns its first character: 'a', 'b', 'a', 'b', 'c'.
  2. Bucket. Words with the same first letter share a sub-list.
  3. Order. Groups follow first appearance (a, b, c), and words keep their original order inside each group.

Example 4 — A Reusable Grouper

Leave out the list to build a named helper, then shape each group with R.map.

Ramda
const byCity = R.collectBy(R.prop('city'));

const people = [
  { name: 'Ana', city: 'Pune' },
  { name: 'Ben', city: 'Delhi' },
  { name: 'Cy',  city: 'Pune' }
];

console.log(R.map(R.pluck('name'), byCity(people)));  // [['Ana', 'Cy'], ['Ben']]
console.log(R.map(R.length, byCity(people)));         // [2, 1]
Try It Yourself

How It Works

  1. Curried. R.collectBy(R.prop('city')) has no list yet, so it returns the function byCity.
  2. Group. byCity(people) gives [[Ana, Cy], [Ben]] — Pune first, then Delhi.
  3. Names. R.map runs R.pluck('name') on each group.
  4. Sizes. R.map(R.length) counts each group: [2, 1].

Example 5 — collectBy vs groupBy vs partition

Same numbers, three grouping helpers, three result shapes.

Ramda
const nums = [1, 2, 3, 4, 5];
const parity = (n) => (n % 2 === 0 ? 'even' : 'odd');
const isEven = (n) => n % 2 === 0;

console.log(R.collectBy(parity, nums));  // [[1, 3, 5], [2, 4]]
console.log(R.groupBy(parity, nums));    // { odd: [1, 3, 5], even: [2, 4] }
console.log(R.partition(isEven, nums));  // [[2, 4], [1, 3, 5]]
Try It Yourself

How It Works

  1. collectBy. Returns only the groups, in first-seen order (odd first).
  2. groupBy. R.groupBy builds the same groups but keeps the keys in an object.
  3. partition. R.partition takes a true/false test and always returns passing items first.
  4. Pick by shape. Need names? groupBy. Need just two buckets? partition. Need any number of plain lists? collectBy.

Browser & Runtime Support

R.collectBy() is plain JavaScript inside Ramda 0.28.0+. It uses no browser API and does not need native Object.groupBy (ES2024), so support depends only on loading Ramda.

✓ Ramda 0.28.0+ · Any JS runtime

Ramda.js R.collectBy()

The UMD build (ramda.min.js) is compiled to ES5 syntax, so it runs in Chrome, Firefox, Safari, and Edge. Via npm it works in Node.js, Deno, and Bun with require or import.

100% With Ramda 0.28+ loaded
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
R.collectBy() Universal

Bottom line: Safe in any project with Ramda 0.28.0 or later (check with npm ls ramda). Load ramda.min.js from a CDN in the browser, or npm install ramda for Node, Deno, and Bun. These labs load [email protected].

Frequently Asked Questions

R.collectBy(fn, list) calls fn on every item and puts items with the same result into the same sub-list. It returns an array of those sub-lists, without the keys. The official sample groups meals into breakfasts, lunches, and dinners.
Both group the same way. R.groupBy returns an object keyed by the group name, such as { odd: [1, 3, 5], even: [2, 4] }. R.collectBy returns only the groups: [[1, 3, 5], [2, 4]]. Use groupBy when you need to look groups up by name.
R.partition always returns exactly two lists: items that pass a predicate, then items that fail. R.collectBy returns one list per distinct key, so it can make any number of groups.
For text keys, groups appear in the order their key is first seen, and items keep their original order inside each group. Keys that look like whole numbers, such as 1 or '7', are sorted in ascending order first, because Ramda stores groups in a plain object.
No. It returns a new outer array and new inner arrays. The items themselves are not copied, so objects in the groups are the same references as in the input list.
No. R.collectBy has been a stable Ramda list function since v0.28.0. It is not deprecated, experimental, or non-standard. Older Ramda versions do not have it.

Next: R.concat()

Join two lists or two strings into one new value.

R.concat() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful