Ramda.js R.indexBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.indexBy(fn, list) turns a list into an object. It calls fn on each item to get a key, then stores that item under the key (official Ramda docs). The result is a lookup table: read result[id] instead of searching the list.

Stable since v0.19.0: R.indexBy is not deprecated, experimental, or non-standard. Note: when two items share a key, only the last one is kept.

Syntax

Call R.indexBy. Start with this line.

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

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

Library declarations
Ramda
R.indexBy(fn, list)

Parameters

ParameterDescription
fnRequired. Function. Receives one item and returns its key (a string, number, or symbol)
listOptional. Array. The items to index; omit it to get a reusable function

Return Value

Return ValueDescription
*The value returned by R.indexBy().

Ramda Examples

Example 1 — Official Sample

Turn two records into an object keyed by their id, as in the Ramda docs.

Ramda
const list = [{ id: 'xyz', title: 'A' }, { id: 'abc', title: 'B' }];

const byId = R.indexBy(R.prop('id'), list);

console.log(JSON.stringify(byId));
Try It Yourself

How It Works

  1. Key function. R.prop('id') returns 'xyz' for the first item and 'abc' for the second.
  2. Store. Each whole item is saved under its key.
  3. Order. Keys appear in list order (xyz first). The docs comment lists abc first, but the contents are identical.
  4. No mutation. list is unchanged; byId is a new object.

Example 2 — Look Up by Id Instead of Searching

Index once, then read any user directly by key.

Ramda
const users = [
  { id: 'u1', name: 'Ann' },
  { id: 'u2', name: 'Bob' },
  { id: 'u3', name: 'Cy' }
];

const usersById = R.indexBy(R.prop('id'), users);

console.log(usersById.u2.name);    // Bob
console.log(usersById['u3'].name); // Cy
console.log(usersById.u9);         // undefined
Try It Yourself

How It Works

  1. Index once. usersById has the keys u1, u2, and u3.
  2. Dot or brackets. usersById.u2 and usersById['u3'] read an item directly.
  3. Missing key. No user has id u9, so the result is undefined.
  4. Why bother. Each lookup is instant, while R.find scans the list every time.

Example 3 — Duplicate Keys: Last One Wins

Two people share id 'a'. Only one of them survives.

Ramda
const people = [
  { id: 'a', name: 'Ann' },
  { id: 'b', name: 'Bob' },
  { id: 'a', name: 'Ada' }
];

const byId = R.indexBy(R.prop('id'), people);

console.log(byId.a.name);                       // Ada
console.log(JSON.stringify(Object.keys(byId))); // ["a","b"]
Try It Yourself

How It Works

  1. First write. Ann is stored under a.
  2. Overwrite. Ada also has key a, so she replaces Ann.
  3. Position kept. Key a still comes before b; only its value changed.
  4. Need both? Use R.groupBy (Example 5).

Example 4 — A Custom Key Function

The key can be computed. Here, country codes become uppercase keys.

Ramda
const countries = [
  { code: 'in', name: 'India' },
  { code: 'jp', name: 'Japan' }
];

const byCode = R.indexBy(R.pipe(R.prop('code'), R.toUpper), countries);

console.log(JSON.stringify(Object.keys(byCode))); // ["IN","JP"]
console.log(byCode.JP.name);                      // Japan
Try It Yourself

How It Works

  1. Build the key. R.pipe reads code, then R.toUpper turns 'in' into 'IN'.
  2. Index. Each country is stored under its uppercase code.
  3. Items unchanged. The stored objects still hold the original lowercase code.
  4. Any function works. As long as it returns a string or number, it can be a key.

Example 5 — R.indexBy vs R.groupBy

Same people, same key function, two different results.

Ramda
const people = [
  { id: 'a', name: 'Ann' },
  { id: 'b', name: 'Bob' },
  { id: 'a', name: 'Ada' }
];

const one = R.indexBy(R.prop('id'), people);
const all = R.groupBy(R.prop('id'), people);

console.log(one.a.name);                             // Ada
console.log(JSON.stringify(R.pluck('name', all.a))); // ["Ann","Ada"]
Try It Yourself

How It Works

  1. indexBy. One item per key, so one.a is just Ada.
  2. groupBy. R.groupBy keeps every match, so all.a is an array of Ann and Ada.
  3. Read names. R.pluck('name') pulls the names out of that array.
  4. Rule of thumb. Unique ids → indexBy; shared categories → groupBy.

Browser & Runtime Support

R.indexBy() is plain JavaScript inside Ramda 0.19.0+. It needs no newer browser API, so support depends only on loading Ramda.

✓ Ramda 0.19.0+ · Any JS runtime

Ramda.js R.indexBy()

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.19+ 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.indexBy() Universal

Bottom line: Safe in any project with Ramda 0.19.0 or later (check with npm ls ramda). The native alternative, Object.fromEntries, needs ES2019: Chrome 73+, Firefox 63+, Safari 12.1+, Node 12+. These labs load [email protected].

Frequently Asked Questions

R.indexBy(fn, list) turns a list into an object. For each item it calls fn(item) to get a key and stores the item under that key, so you can later read result[key] instead of searching the list.
The last item wins. It overwrites the earlier one, and the key keeps the position where it first appeared. If you need every matching item, use R.groupBy instead.
Both use a key function. R.indexBy stores one item per key, while R.groupBy stores an array of all items that share the key. Use indexBy for unique ids and groupBy for shared categories.
Use the key: byId.u2 or byId['u2']. A key that was never produced gives undefined, so for a safe default use R.propOr(fallback, key, byId).
Object.fromEntries(list.map(x => [x.id, x])) builds the same object in ES2019+ (Chrome 73, Firefox 63, Safari 12.1, Node 12). It also keeps the last duplicate. R.indexBy is curried and takes the key function first, which suits pipelines.
No. R.indexBy has been a stable Ramda List function since v0.19.0. It is not deprecated, experimental, or non-standard.

Next: R.indexOf()

Find the position of the first matching value in a list.

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