2024-06-30 06:17:00 +03:00
|
|
|
# keyBy
|
|
|
|
|
|
|
|
Maps each element of an array based on a provided key-generating function.
|
|
|
|
|
|
|
|
This function takes an array and a function that generates a key from each element. It returns
|
|
|
|
an object where the keys are the generated keys and the values are the corresponding elements.
|
|
|
|
If there are multiple elements generating the same key, the last element among them is used as the value.
|
|
|
|
|
|
|
|
## Signature
|
|
|
|
|
|
|
|
```typescript
|
2024-07-14 11:31:45 +03:00
|
|
|
function keyBy<T, K extends PropertyKey>(arr: T[], getKeyFromItem: (item: T) => K): Record<K, T>;
|
2024-06-30 06:17:00 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
### Parameters
|
|
|
|
|
|
|
|
- `arr` (`T[]`): The array of elements to be mapped.
|
|
|
|
- `getKeyFromItem` (`(item: T) => K`): A function that generates a key from an element.
|
|
|
|
|
|
|
|
### Returns
|
|
|
|
|
|
|
|
(`Record<K, T>`) An object where keys are mapped to each element of an array.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
const array = [
|
|
|
|
{ category: 'fruit', name: 'apple' },
|
|
|
|
{ category: 'fruit', name: 'banana' },
|
|
|
|
{ category: 'vegetable', name: 'carrot' },
|
|
|
|
];
|
|
|
|
const result = keyBy(array, item => item.category);
|
|
|
|
// result will be:
|
|
|
|
// {
|
|
|
|
// fruit: { category: 'fruit', name: 'banana' },
|
|
|
|
// vegetable: { category: 'vegetable', name: 'carrot' }
|
|
|
|
// }
|
|
|
|
```
|