Skip to content

mapKeys

Creates a new object with the same values as the given object, but with keys generated by running each own enumerable property of the object through the getNewKey function.

Signature

typescript
function mapKeys<T extends Record<PropertyKey, unknown>, K1 extends keyof T, K2 extends PropertyKey>(
  object: T,
  getNewKey: (value: T[K1], key: K1, object: T) => N
): Record<K2, T[K]>;

Parameters

  • obj (T extends object): The object to iterate over.
  • getNewKey: ((value: T[K1], key: K1, object: T) => N): The function invoked per own enumerable property.

Returns

(Record<K2, T[K]>): The new mapped object.

Examples

typescript
const obj = { a: 1, b: 2 };
const result = mapKeys(obj, (value, key) => key + value);
console.log(result); // { a1: 1, b2: 2 }

Performance Comparison

Bundle SizePerformance
es-toolkit138 bytes (99.1% smaller)2,844,013 times (11% faster)
es-toolkit/compat1,124 bytes (93.2% smaller)2,899,524 times (13% faster)
lodash-es16,649 bytes2,559,949 times

Released under the MIT License.