Ramda.js R.uniqBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.uniqBy(fn, list) removes duplicates by a key. It calls fn on every item to get a key, and keeps only the first item for each distinct key (official Ramda docs).

Stable since v0.16.0: R.uniqBy is not deprecated, experimental, or non-standard. Note: it keeps the first item for each key and returns the original items, never the keys.

Syntax

Call R.uniqBy. Start with this line.

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

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

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

Parameters

ParameterDescription
fnRequired. Called once per item; returns the key to compare
listOptional. The array to clean up; omit it to get a reusable function

Return Value

Return ValueDescription
*A new array of original items, one per distinct key

Ramda Examples

Example 1 — Official: Unique by Absolute Value

Treat numbers the same distance from zero as duplicates.

Ramda
console.log(R.uniqBy(Math.abs, [-1, -5, 2, 10, 1, 2]));  // [-1, -5, 2, 10]
Try It Yourself

How It Works

  1. Keys. Math.abs turns the list into keys 1, 5, 2, 10, 1, 2.
  2. New keys. The first four keys are new, so -1, -5, 2 and 10 are kept.
  3. Repeated keys. 1 and the second 2 produce keys already seen, so they are dropped.
  4. Originals. The result holds -1 and -5, not their keys 1 and 5.

Example 2 — Remove Duplicate Records by id

Two objects with the same id are the same record, even if other fields differ.

Ramda
const users = [
  { id: 1, name: 'Ana' },
  { id: 2, name: 'Ben' },
  { id: 1, name: 'Ana Lee' }
];

console.log(R.uniqBy(R.prop('id'), users));
// [{ id: 1, name: 'Ana' }, { id: 2, name: 'Ben' }]
Try It Yourself

How It Works

  1. Key. R.prop('id') reads each user’s id: 1, 2, 1.
  2. First wins. The first user with id: 1 ('Ana') is kept.
  3. Later copy dropped. 'Ana Lee' also has id: 1, so it is removed even though the name differs.
  4. Compare with uniq. R.uniq(users) would keep all three, because no two objects are equal in every field.

Example 3 — Unique Words, Ignoring Case

Use a lowercase version of each word as the key, but keep the word as it was first written.

Ramda
console.log(R.uniqBy(R.toLower, ['Apple', 'apple', 'Banana', 'APPLE']));
// ['Apple', 'Banana']
Try It Yourself

How It Works

  1. Keys. R.toLower gives 'apple', 'apple', 'banana', 'apple'.
  2. First spelling wins. 'Apple' is the first item with key 'apple', so it stays.
  3. Other spellings dropped. 'apple' and 'APPLE' share that key and are removed.
  4. Original text. The result shows 'Apple' with its capital letter, not the lowercase key.

Example 4 — Keep the Latest Version of Each Record

R.uniqBy keeps the first item per key. Reverse before and after to keep the last one.

Ramda
const edits = [
  { id: 1, text: 'draft' },
  { id: 2, text: 'hello' },
  { id: 1, text: 'final' }
];

const latestById = R.pipe(R.reverse, R.uniqBy(R.prop('id')), R.reverse);

console.log(latestById(edits));
// [{ id: 2, text: 'hello' }, { id: 1, text: 'final' }]
Try It Yourself

How It Works

  1. Reverse. R.reverse puts 'final' first: final, hello, draft.
  2. Dedupe. R.uniqBy(R.prop('id')) keeps 'final' for id 1 and drops 'draft'.
  3. Reverse back. The survivors return to their original relative order: 'hello', then 'final'.
  4. Safe. R.reverse returns a new array, so edits is not changed.

Example 5 — Unique by Several Fields

One booking per room per day: use both fields together as the key.

Ramda
const bookings = [
  { room: 'A', date: 'Mon', guest: 'Ana' },
  { room: 'A', date: 'Tue', guest: 'Ben' },
  { room: 'A', date: 'Mon', guest: 'Cy' },
  { room: 'B', date: 'Mon', guest: 'Dee' }
];

const firstPerSlot = R.uniqBy(R.props(['room', 'date']), bookings);

console.log(R.pluck('guest', firstPerSlot));  // ['Ana', 'Ben', 'Dee']
Try It Yourself

How It Works

  1. Pair key. R.props(['room', 'date']) turns each booking into a key like ['A', 'Mon'].
  2. Deep match. Keys are compared with R.equals, so Cy’s ['A', 'Mon'] matches Ana’s and is dropped.
  3. Different slots stay. ['A', 'Tue'] and ['B', 'Mon'] are new keys, so Ben and Dee stay.
  4. Show names. R.pluck('guest') lists who got each slot.

Browser & Runtime Support

R.uniqBy() is plain ES5 JavaScript inside Ramda 0.16.0+. Ramda uses a native Set to speed up plain keys when one exists, and falls back to its own lookup table when it does not — the results are identical either way.

✓ Ramda 0.16.0+ · Any JS runtime

Ramda.js R.uniqBy()

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. The examples on this page only add const (ES2015), which every modern browser supports.

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

Bottom line: Safe in any project with Ramda 0.16.0 or later (check with npm ls ramda). These labs load [email protected], where R.uniqBy also works as a transducer.

Frequently Asked Questions

R.uniqBy(fn, list) removes duplicates based on a key. It calls fn on every item and keeps only the first item for each distinct key, in the original order. The official sample R.uniqBy(Math.abs, [-1, -5, 2, 10, 1, 2]) returns [-1, -5, 2, 10].
The original items. The key is only used to decide which items are duplicates. That is why the official result starts with -1, not with Math.abs(-1), which is 1.
Reverse the list, run R.uniqBy, then reverse again: R.pipe(R.reverse, R.uniqBy(R.prop('id')), R.reverse). R.uniqBy always keeps the first item it meets, so reversing makes the last item come first.
R.uniq compares whole items with R.equals. R.uniqBy compares a key computed from each item, and R.uniq is the same as R.uniqBy(R.identity). R.uniqWith takes a function of two items that returns true when they count as duplicates.
Yes. Return both fields as the key, for example R.uniqBy(R.props(['room', 'date']), list). Keys are compared with R.equals, so arrays with the same contents match. For large lists, a joined string key such as room + '|' + date is faster.
No. R.uniqBy has been a stable Ramda list function since v0.16.0. It is not deprecated, experimental, or non-standard.

Next: R.uniqWith()

Remove duplicates using a function that compares two items.

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