Ramda.js R.reduceBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.reduceBy(valueFn, acc, keyFn, list) groups a list by a key and reduces each group to one value, all in a single pass. keyFn names the group for every item, and valueFn(groupAcc, item) updates that group’s value (official Ramda docs). Think of it as R.groupBy and R.reduce combined.

Stable since v0.20.0: R.reduceBy is not deprecated, experimental, or non-standard. It is the general tool behind R.groupBy, R.countBy, and R.indexBy.

Syntax

Call R.reduceBy. Start with this line.

Ramda
R.reduceBy(valueFn, acc, keyFn, list) or R.reduceBy(valueFn, acc, keyFn)(list)
CallWhat it does
R.reduceBy(valueFn, acc, keyFn, list) or R.reduceBy(valueFn, acc, keyFn)(list)Primary signature.
R.reduceBy(...)Call the list helper.

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

Library declarations
Ramda
R.reduceBy(valueFn, acc, keyFn, list) or R.reduceBy(valueFn, acc, keyFn)(list)

Parameters

ParameterDescription
valueFnRequired. Called as valueFn(groupAcc, item); returns the group's new value
accRequired. Starting value for each new group (copied per group)
keyFnRequired. Returns the group name for an item; non-strings become string keys
listOptional. The array to walk; omit it to get a reusable function

Return Value

Return ValueDescription
*A new object of { key: foldedValue }; the input is never changed

Ramda Examples

Example 1 — Official Names by Grade

The sample from the Ramda docs: group students by grade and keep only their names.

Ramda
const groupNames = (acc, { name }) => acc.concat(name);
const toGrade = ({ score }) =>
  score < 65 ? 'F' :
  score < 70 ? 'D' :
  score < 80 ? 'C' :
  score < 90 ? 'B' : 'A';

const students = [
  { name: 'Abby', score: 83 },
  { name: 'Bart', score: 62 },
  { name: 'Curt', score: 88 },
  { name: 'Dora', score: 92 }
];

console.log(R.reduceBy(groupNames, [], toGrade, students));
// { B: ['Abby', 'Curt'], F: ['Bart'], A: ['Dora'] }
Try It Yourself

How It Works

  1. Group. toGrade turns each score into a letter: Abby 83 → B, Bart 62 → F, Curt 88 → B, Dora 92 → A.
  2. Fresh start. The first time a grade appears, its value starts as a new empty array [].
  3. Fold. groupNames adds the student’s name with concat, so B grows from ['Abby'] to ['Abby', 'Curt'].
  4. Order. Keys follow first appearance (B, F, A), which is why the docs’ A-B-F comment looks different.

Example 2 — Total Amount per Category

Sum order amounts for each type in one pass.

Ramda
const orders = [
  { type: 'food',  amount: 12 },
  { type: 'books', amount: 30 },
  { type: 'food',  amount: 8 },
  { type: 'toys',  amount: 15 },
  { type: 'books', amount: 5 }
];

const addAmount = (sum, order) => sum + order.amount;

console.log(R.reduceBy(addAmount, 0, R.prop('type'), orders));
// { food: 20, books: 35, toys: 15 }
Try It Yourself

How It Works

  1. Key. R.prop('type') reads each order’s category.
  2. Seed. Every new category starts at 0.
  3. Fold. addAmount adds the amount: food 0 + 12 + 8 = 20, books 0 + 30 + 5 = 35, toys 0 + 15 = 15.
  4. One pass. No in-between arrays are built, unlike grouping first and summing later.

Example 3 — Count Items per Group

Count fruits by first letter, then compare with the built-in shortcut.

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

const addOne = (count) => count + 1;

console.log(R.reduceBy(addOne, 0, R.head, fruits));  // { a: 2, b: 2, c: 1 }
console.log(R.countBy(R.head, fruits));              // { a: 2, b: 2, c: 1 }
Try It Yourself

How It Works

  1. Key. R.head on a string returns its first letter: 'apple' → 'a'.
  2. Ignore the item. addOne only uses the count; the fruit itself does not matter.
  3. Fold. Each group starts at 0 and goes up by 1 per item.
  4. Same thing. R.countBy is literally built as R.reduceBy((acc) => acc + 1, 0).

Example 4 — Best Score per Team

A group’s value can be anything — here, the highest score seen so far.

Ramda
const players = [
  { team: 'red',  score: 7 },
  { team: 'blue', score: 12 },
  { team: 'red',  score: 15 },
  { team: 'blue', score: 9 }
];

const keepBest = (best, player) => R.max(best, player.score);

console.log(R.reduceBy(keepBest, -Infinity, R.prop('team'), players));
// { red: 15, blue: 12 }
Try It Yourself

How It Works

  1. Seed. Each team starts at -Infinity, so any real score is bigger.
  2. Red. R.max(-Infinity, 7) is 7, then R.max(7, 15) is 15.
  3. Blue. R.max(-Infinity, 12) is 12, then R.max(12, 9) stays 12.
  4. Why not 0? A seed of 0 would give the wrong answer if every score in a team were negative.

Example 5 — Reusable in a Pipeline

Leave out the list to get a function, then use it after a filter.

Ramda
const orders = [
  { type: 'food',  amount: 12, paid: true },
  { type: 'books', amount: 30, paid: false },
  { type: 'food',  amount: 8,  paid: true },
  { type: 'books', amount: 5,  paid: true }
];

const totalByType = R.reduceBy((sum, o) => sum + o.amount, 0, R.prop('type'));

const paidTotals = R.pipe(
  R.filter(R.prop('paid')),
  totalByType
);

console.log(paidTotals(orders));
// { food: 20, books: 5 }
Try It Yourself

How It Works

  1. Curried. Three arguments given, list missing, so totalByType is a function waiting for a list.
  2. Filter. R.filter(R.prop('paid')) drops the unpaid 30 book order.
  3. Group and sum. food 12 + 8 = 20, books 5.
  4. Pipeline. R.pipe passes the filtered list straight into totalByType.

Browser & Runtime Support

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

✓ Ramda 0.20.0+ · Any JS runtime

Ramda.js R.reduceBy()

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.20+ 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.reduceBy() Universal

Bottom line: Safe in any project with Ramda 0.20.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.reduceBy(valueFn, acc, keyFn, list) puts each item into a group named by keyFn(item), then folds every group into one value with valueFn(groupAcc, item). Each group starts from its own copy of acc. The result is an object of key to folded value.
R.groupBy always collects the original items into arrays. R.reduceBy lets you turn each group into anything: a sum, a count, a maximum, or a list of names. In fact, Ramda builds R.groupBy, R.countBy, and R.indexBy on top of R.reduceBy.
An empty object {}. No items means no keys, so the seed acc is never used. R.reduceBy(R.add, 0, R.identity, null) also returns {}. This differs from R.reduce, which returns the seed itself.
No. Every new key starts from a fresh shallow copy of acc, so a seed of [] or {} is never shared between groups or between calls. Nested objects inside the seed are still shared, so returning new values with concat or assoc is the safest habit.
Keys appear in the order each group is first seen, except integer-like keys such as '1' and '10', which JavaScript always lists in ascending order. The official sample returns B, F, A because Abby (B) comes first.
No. R.reduceBy has been a stable Ramda list function since v0.20.0. It is not deprecated, experimental, or non-standard.

Next: R.reduced()

Wrap a value to stop a reduce before the list ends.

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