Skip to content

mapKeys() function

Returns a new object with the same values as obj, where each key is the result of applying f to the original value and key. When multiple keys map to the same new key, the last one wins.

Signature:

typescript
declare function mapKeys<T extends object, K2 extends PropertyKey>(f: (value: T[keyof T], key: keyof T & string) => Promise<K2>, obj: T): Promise<Record<K2, T[keyof T]>>;

Example

ts
mapKeys((v, k) => k.toUpperCase(), { a: 1, b: 2 }); // { A: 1, B: 2 }
mapKeys((v) => String(v), { a: 1 }); // { "1": 1 }

// asynchronous callback
await mapKeys(async (v, k) => k.toUpperCase(), { a: 1 }); // { A: 1 }

// with pipe
pipe(
  { a: 1, b: 2 },
  mapKeys((v, k) => k.toUpperCase()),
); // { A: 1, B: 2 }

Open Source Code

Released under the Apache-2.0 License.