Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | export function keyBy<
A extends object,
K extends keyof {
[P in keyof A as A[P] extends PropertyKey ? P : never]: unknown;
}
>(array: A[], key: K) {
return array.reduce(
(r, x) => ({ ...r, [x[key] as unknown as PropertyKey]: x }),
{} as { [P in A[K] as A[K] extends PropertyKey ? A[K] : never]: A }
);
}
export function keyByFunction<A extends object, K extends PropertyKey>(
array: A[],
keyFn: (x: A) => K
) {
return array.reduce(
(r, x) => ({ ...r, [keyFn(x)]: x }),
{} as { [P in K]: A }
);
}
export default keyBy;
|