Ramda’s R.renameKeys() answers a dictionary question: how do I rename a few own keys and leave the rest? You pass a mapping of 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. mapping = {name: 'firstName', address: 'street'} and obj = {name: 'John', city: 'Paris'}. R.renameKeys(mapping, obj) is {firstName: 'John', city: 'Paris'}. Need a function on every key? That is R.mapKeys. Need the lower-level rebuild of pairs? That is R.rebuild. Need to rewrite values instead? That is R.map.
Syntax
Call R.renameKeys. Start with this line.
Ramda
R.renameKeys(mapping, obj)
Call
What it does
R.renameKeys(mapping, obj)
Primary signature.
R.renameKeys(...)
Call the object helper.
Prefer import { renameKeys } from "ramda" when you want a named import.
Library declarations
Ramda
R.renameKeys(mapping, obj)
Parameters
Parameter
Description
mapping
Required. Object. Pairs of existing keys with new ones, as {oldKey: newKey}. Extra names that are not on obj are ignored.
obj
Required. Object. The record whose own keys should be rewritten. Prototype names are skipped. The source is never mutated.
Return Value
Return Value
Description
Object
A new object. Mapped keys use the new names. Unmapped keys keep the old names. Values stay. If two keys become one name, the last processed value wins.
Ramda Examples
📚 Getting Started
The official John / Paris sample, then unmapped keys, extra mapping names, and no mutation.
Example 1 — Official Rename Mapping
name becomes firstName. city is not in the mapping, so it stays. Unused address does nothing.
Ramda looks up each own key in mapping. name is present, so it becomes firstName. city is not, so it stays. address is only in the mapping, so it never appears. That is the docs sample from ramdajs.com.
Example 2 — Unmapped Keys and No Mutation
The leftover is a new object. The source still has name. Extra mapping names are ignored.
renameKeys always allocates a new object. Nested values stay shared by reference, the same shallow rule as mapKeys. The mapping is a lookup, not a whitelist of leftover keys.
📈 Practical Patterns
Compare mapKeys, then a curried helper mapped over rows, then collisions and an empty mapping.
Example 3 — vs mapKeys
A dictionary changes listed names. A function changes every name.
Internally, renameKeys is mapKeys with a lookup: if the old key is in the mapping, use the new name, else keep the old name. Need every key uppercased? Stay on R.mapKeys.
Example 4 — Curry and Map
Fill the mapping to map over rows. Each leftover uses firstName.
Own-key order is usually insertion order. b is processed after a, so x is 2. An empty mapping never finds a hit, so every own key is copied. There is nothing to rename on {}.
Compatibility
Browser Support
R.renameKeys() 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.renameKeys()
Load Ramda 0.31.0 or later via CDN or npm, then call R.renameKeys with a mapping object and a record. No extra browser API is required.
100%With Ramda 0.31.0+
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
R.renameKeys()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 renameKeys.
Frequently Asked Questions
R.renameKeys(mapping, obj) returns a new object whose own keys are renamed when they appear in mapping as {oldKey: newKey}. Unmapped keys stay. Added in v0.31.0. The original object is not mutated.
mapping = {name: 'firstName', address: 'street'} and obj = {name: 'John', city: 'Paris'}. R.renameKeys(mapping, obj) is {firstName: 'John', city: 'Paris'}.
renameKeys uses a dictionary of old names to new names. mapKeys uses a function on every key. rebuild is the lower-level rebuild of key-value pairs. Internally renameKeys is mapKeys with has(key, mapping) ? mapping[key] : key.
They keep their old names and values. Extra names in the mapping that are not on the object are ignored.
The last processed key wins, because renameKeys is built on mapKeys. Earlier values for that new name are dropped.
No. R.renameKeys is a stable Ramda object helper from v0.31.0. It is not deprecated, experimental, or non-standard. It is missing from Ramda 0.30.x.