UNPKG

typedash

Version:

modern, type-safe collection of utility functions

27 lines (25 loc) 825 B
/** * Creates an object composed of keys generated from the results of running each element of `array` through `keyGetter`. * The corresponding value of each key is the last element responsible for generating the key. * @param array The array to iterate over. * @param keyGetter The function used to extract the key from each element. * @returns An object with the keys mapped to the elements. * @example * ```ts * keyBy( * [ * { id: 'a', value: 1 }, * { id: 'b', value: 2 }, * { id: 'c', value: 3 }, * ], * (item) => item.id * ) * // { * // a: { id: 'a', value: 1 }, * // b: { id: 'b', value: 2 }, * // c: { id: 'c', value: 3 } * // } * ``` */ declare function keyBy<T, K extends PropertyKey>(array: readonly T[], keyGetter: (item: T) => K): Partial<Record<K, T>>; export { keyBy };