Ramda.js R.mapKeys() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.mapKeys() answers a naming question: how do I rewrite every own key and keep the values? You pass a function from old name to new name, then an object. You get a new object. The original is not mutated.

The official sample is the whole idea. R.mapKeys(R.toUpper, {foo: 1, bar: 2, baz: 3}) is {FOO: 1, BAR: 2, BAZ: 3}. Need to rewrite values instead? That is R.map. Need the key while mapping a value? That is the next helper, R.mapObjIndexed. Need a dictionary of old name to new name? That is R.renameKeys. The previous Object helper is R.lensProp, which focuses one key rather than renaming all of them.

Syntax

Call R.mapKeys. Start with this line.

Ramda
R.mapKeys(fn, obj)
CallWhat it does
R.mapKeys(fn, obj)Primary signature.
R.mapKeys(...)Call the object helper.

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

Library declarations
Ramda
R.mapKeys(fn, obj)

Parameters

ParameterDescription
fnRequired. Function. Given an own key, return the new key. Official: toUpper. It does not receive the value.
objRequired. Object. The record whose own keys should be rewritten. Prototype names are skipped.

Return Value

Return ValueDescription
ObjectA new object with transformed keys and the same values. If two keys become one name, the last processed value wins. The source is not mutated.

Ramda Examples

📚 Getting Started

The official uppercase sample, then the same object with map rewriting values instead.

Example 1 — Official toUpper

Every own key is uppercased. Values stay 1, 2, and 3.

Ramda
R.mapKeys(R.toUpper, {foo: 1, bar: 2, baz: 3});
//=> {FOO: 1, BAR: 2, BAZ: 3}
Try It Yourself

How It Works

The docs write toUpper without R. because the REPL already has it in scope. In an app, use R.toUpper. That is the sample from ramdajs.com.

Example 2 — vs R.map

mapKeys changes names. map changes numbers.

Ramda
const data = {foo: 1, bar: 2};

R.mapKeys(R.toUpper, data);
//=> {FOO: 1, BAR: 2}
R.map(R.inc, data);
//=> {foo: 2, bar: 3}
Try It Yourself

How It Works

Need both a new name and a new value? Map keys first, then map values, or use R.rebuild for a custom pair list.

📈 Practical Patterns

A collision where the last key wins, a prefix, then a curried helper in a pipe.

Example 3 — Colliding Keys

apple and apricot both become a. The last processed value stays.

Ramda
const firstLetter = (key) => key[0];
R.mapKeys(firstLetter, {apple: 1, apricot: 2, banana: 3});
//=> {a: 2, b: 3}
Try It Yourself

How It Works

The docs: if multiple keys map to the same new key, the last one processed will dominate. Here apricot is processed after apple, so a is 2, not 1.

Example 4 — Prefix Keys

concat is already curried. The original user stays put.

Ramda
const user = {id: 1, name: 'Ada'};
R.mapKeys(R.concat('user_'), user);
//=> {user_id: 1, user_name: 'Ada'}
user;
//=> {id: 1, name: 'Ada'}
Try It Yourself

How It Works

R.concat('user_') waits for the old key. Nested objects are not walked. Only own keys on this record change.

Example 5 — Curry with pipe

Uppercase the names, then list them. Both helpers are data-last.

Ramda
const shoutKeys = R.mapKeys(R.toUpper);
const shoutKeyNames = R.pipe(R.mapKeys(R.toUpper), R.keys);
const data = {foo: 1, bar: 2};

shoutKeys(data);
//=> {FOO: 1, BAR: 2}
shoutKeyNames(data);
//=> ['FOO', 'BAR']
Try It Yourself

How It Works

R.mapKeys(R.toUpper) waits for the object. Drop it at the start of R.pipe when a later step needs the new names.

Browser Support

R.mapKeys() is ordinary JavaScript once Ramda 0.31 or later is loaded. Logos use the shared browser-image-sprite.png sprite from this project.

✓ Ramda 0.31.0+ · Any JS runtime

Ramda.js R.mapKeys()

Load Ramda 0.31.0 or later via CDN or npm, then call R.mapKeys with a key function and an object. No extra browser API is required.

100% With Ramda 0.31.0+
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
R.mapKeys() Universal

Bottom line: Safe in any project that includes Ramda 0.31.0 or later. Use a CDN build such as ramda.min.js in the browser or install ramda from npm for Node. These labs use [email protected]. Ramda 0.30.x does not include mapKeys.

Frequently Asked Questions

R.mapKeys(fn, obj) returns a new object whose own keys are fn(oldKey). Values are copied as they are. Added in v0.31.0. The original object is not mutated.
R.mapKeys(toUpper, {foo: 1, bar: 2, baz: 3}) is {FOO: 1, BAR: 2, BAZ: 3}. In these labs that is R.toUpper.
map transforms values and keeps the same keys. mapKeys transforms keys and keeps the same values. mapObjIndexed maps values with (value, key, obj) and still keeps the original keys.
The last one processed wins. The docs: if multiple keys map to the same new key, the last one processed will dominate. Values from earlier collisions are dropped.
No. The type is (String -> String). The function only sees the old key. Lodash _.mapKeys is different: its iteratee is often (value, key).
No. R.mapKeys is a stable Ramda object helper from v0.31.0. It is not deprecated, experimental, or non-standard.

Continue to R.mapObjIndexed()

Learn how to map every own value with its key using Ramda R.mapObjIndexed().

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