# Immutably rename object keys

Return a copy with keys renamed through a mapping.

Canonical: https://1loc.dev/object/immutably-rename-object-keys/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function renameKeys({ keysMap, object }) { return Object.fromEntries(Object.entries(object).map(([key, value]) => [keysMap[key] || key, value])); }
```

## Input

```json
{
  "keysMap": {
    "idempotency_key": "idempotencyKey"
  },
  "object": {
    "to": "ana@example.com",
    "idempotency_key": "order-confirmation:8812:1"
  }
}
```

## Expected output

```json
{
  "to": "ana@example.com",
  "idempotencyKey": "order-confirmation:8812:1"
}
```

## Edge case

Input:
```json
{
  "keysMap": {},
  "object": {}
}
```
Output:
```json
{}
```

## Limits

- Object.fromEntries replaces the original reduce with spread, which copied the accumulator on every key.
- If two keys map to the same name, the later one wins.

The lab validates types and size before calling this function. Add equivalent validation at your own trust boundary.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/immutably-rename-object-keys.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: imyangyong
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Uses Object.fromEntries instead of spreading the accumulator for every key. Original MDX retained as inert source evidence.
