mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-28 03:34:26 +03:00
1.3 KiB
1.3 KiB
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 }
Performance Comparison
Bundle Size | Performance | |
---|---|---|
es-toolkit | 138 bytes (99.1% smaller) | 2,844,013 times (11% faster) |
es-toolkit/compat | 1,124 bytes (93.2% smaller) | 2,899,524 times (13% faster) |
lodash-es | 16,649 bytes | 2,559,949 times |