es-toolkit/docs/reference/object/mapKeys.md
seungrodotlee 13c7c7f611
feat(mapKeys,mapValues): add mapKeys and mapValues (#291)
* feat(mapKeys): add mapKeys

* feat(map*): not clone object

* chore. fix names on bench

* feat. pass cloned object to iteratee

* Fix interface

* fix. fix test codes

* fix. fix type error on test

* Add docs

* test: Check test

* bench

* rewrite

* mapKeys

* test: Do not mutate the original function

---------

Co-authored-by: raon0211 <raon0211@toss.im>
2024-07-25 11:43:15 +09:00

811 B

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

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

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