Ramda.js R.groupBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.groupBy(fn, list) splits a list into a new object of arrays. It calls fn on every item; the return value becomes a key, and every item with that key lands in the same array (official Ramda docs). The source list is never changed.

Stable since v0.1.0: R.groupBy is not deprecated, experimental, or non-standard. The native Object.groupBy() (ES2024) does the same job data-first, but only in newer runtimes.

Syntax

Call R.groupBy. Start with this line.

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

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

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

Parameters

ParameterDescription
fnRequired. Function. Called with one item; returns the key (a string, number, or symbol)
listOptional. Array. The items to group; omit it to get a reusable function

Return Value

Return ValueDescription
*A new object: { key: [items] }

Ramda Examples

Example 1 — Official Students by Grade

Turn each score into a letter, then group the students by that letter — the sample from the Ramda docs.

Ramda
const byGrade = R.groupBy((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: 'Dianne', score: 99 },
  { name: 'Jack', score: 69 }
];

console.log(JSON.stringify(byGrade(students)));
// { B: [Abby], F: [Eddy], A: [Dianne], D: [Jack] }
Try It Yourself

How It Works

  1. Curry first. R.groupBy(fn) has no list yet, so it returns the reusable helper byGrade.
  2. Label each student. 84 → 'B', 58 → 'F', 99 → 'A', 69 → 'D'.
  3. Group by label. Each student goes into the array under their letter.
  4. Only real keys. No one scored a C, so there is no C key at all.

Example 2 — Group Objects by a Field

The everyday case: R.prop('status') is the key function, so tasks are grouped by their status.

Ramda
const tasks = [
  { title: 'Write intro', status: 'done' },
  { title: 'Add examples', status: 'todo' },
  { title: 'Fix typo', status: 'done' },
  { title: 'Publish', status: 'todo' }
];

const byStatus = R.groupBy(R.prop('status'), tasks);

console.log(byStatus.done.length);                         // 2
console.log(JSON.stringify(R.pluck('title', byStatus.todo))); // ["Add examples","Publish"]
Try It Yourself

How It Works

  1. Key function. R.prop('status') returns 'done' or 'todo' for each task.
  2. Two groups. byStatus is { done: [2 tasks], todo: [2 tasks] }.
  3. Look up by name. byStatus.done is a normal array, so .length is 2.
  4. Use the group. R.pluck('title') pulls the titles from the todo group.

Example 3 — Custom Labels for Strings and Numbers

Any function that returns a label works — a Ramda helper or your own arrow function.

Ramda
const fruits = ['apple', 'banana', 'avocado', 'cherry', 'blueberry'];
console.log(JSON.stringify(R.groupBy(R.head, fruits)));
// {"a":["apple","avocado"],"b":["banana","blueberry"],"c":["cherry"]}

const parity = (n) => (n % 2 === 0 ? 'even' : 'odd');
console.log(JSON.stringify(R.groupBy(parity, [1, 2, 3, 4, 5])));
// {"odd":[1,3,5],"even":[2,4]}
Try It Yourself

How It Works

  1. First letter. R.head returns the first character of each string, so fruits are grouped by letter.
  2. Order kept. Inside a, 'apple' stays before 'avocado', just like the input.
  3. Your own function. parity returns 'even' or 'odd' for each number.
  4. Readable keys. Returning words beats returning true/false, which would become the keys "true" and "false".

Example 4 — Count Items per Group

Group, then replace each array with its length. Compare it with the R.countBy shortcut.

Ramda
const orders = [
  { id: 1, city: 'Pune' },
  { id: 2, city: 'Delhi' },
  { id: 3, city: 'Pune' },
  { id: 4, city: 'Goa' },
  { id: 5, city: 'Pune' }
];

const countByCity = R.pipe(R.groupBy(R.prop('city')), R.map(R.length));

console.log(JSON.stringify(countByCity(orders)));              // {"Pune":3,"Delhi":1,"Goa":1}
console.log(JSON.stringify(R.countBy(R.prop('city'), orders))); // {"Pune":3,"Delhi":1,"Goa":1}
Try It Yourself

How It Works

  1. Group. R.groupBy(R.prop('city')) builds { Pune: [3 orders], Delhi: [1], Goa: [1] }.
  2. Map the object. R.map also works on objects, so R.length turns each array into a number.
  3. Pipe. R.pipe joins both steps into one reusable function.
  4. Shortcut. R.countBy gives the same counts without building the arrays.

Example 5 — groupBy vs indexBy vs collectBy

Same key function, same words, three different result shapes.

Ramda
const words = ['ant', 'bee', 'ape'];

console.log(JSON.stringify(R.groupBy(R.head, words)));   // {"a":["ant","ape"],"b":["bee"]}
console.log(JSON.stringify(R.indexBy(R.head, words)));   // {"a":"ape","b":"bee"}
console.log(JSON.stringify(R.collectBy(R.head, words))); // [["ant","ape"],["bee"]]
Try It Yourself

How It Works

  1. groupBy keeps all. Both 'ant' and 'ape' stay in the a array.
  2. indexBy keeps one. R.indexBy stores a single item per key, so the later 'ape' replaces 'ant'.
  3. collectBy drops keys. R.collectBy returns just the groups as an array of arrays.
  4. Pick by need. Look up groups by name → groupBy. Unique ids → indexBy. Loop over groups → collectBy.

Browser & Runtime Support

R.groupBy() is plain JavaScript inside Ramda 0.1.0+. It uses no browser API, so support depends only on loading Ramda. (Native Object.groupBy needs Chrome/Edge 117+, Firefox 119+, Safari 17.4+, or Node.js 21+.)

✓ Ramda 0.1.0+ · Any JS runtime

Ramda.js R.groupBy()

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 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.groupBy() Universal

Bottom line: Safe in any project that loads Ramda. Use 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

A new object. Each key is a value returned by your key function, and each value is an array of every item that produced that key, in list order. An empty list returns {}.
R.groupBy keeps every matching item in an array per key. R.indexBy keeps only one item per key (the last one wins). R.collectBy builds the same groups as groupBy but returns an array of arrays with no keys.
Object keys are always strings, and JavaScript lists integer-like keys such as '1', '2', '10' in ascending order before other keys. Other string keys keep first-seen order.
Use R.countBy(fn, list) for a direct count, or R.pipe(R.groupBy(fn), R.map(R.length)) when you also need the groups themselves.
Both group a list into an object of arrays. Object.groupBy (ES2024) is data-first and needs a modern runtime. R.groupBy is function-first and curried, so R.groupBy(fn) drops straight into R.pipe, and it works anywhere Ramda loads.
No. R.groupBy has been a stable Ramda List function since v0.1.0. It is not deprecated, experimental, or non-standard.

Next: R.groupWith()

Group runs of neighbouring items that pass a pairwise test.

R.groupWith() 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