mapKeys
@guoba-ai/utils / object / mapKeys
Function: mapKeys()
function mapKeys<T, K>(obj, fn): Record<K, T[keyof T]>;Defined in: object.ts:252
Transform the keys of an object using a mapping function.
Parameters
obj
T
The source object
fn
(key, value) => K
Function that receives each key and value, returns the new key
Returns
Record<K, T[keyof T]>
A new object with transformed keys
Example
mapKeys({ a: 1, b: 2 }, key => key.toUpperCase()) // { A: 1, B: 2 }
mapKeys({ x: 10, y: 20 }, (key, value) => `${key}_${value}`)
// { x_10: 10, y_20: 20 }
mapKeys({ a: 1, b: 2 }, () => 'same') // { same: 2 }Warning
If the mapping function returns the same key for multiple entries, the later value wins.