> For AI agents: the complete documentation index is available at https://massaman.dev/llms.txt, the full documentation bundle is available at https://massaman.dev/llms-full.txt.

# keyBy (Functional Programming) es-toolkit

Creates a function that builds an object keyed by each value. Use it with [`pipe`](https://massaman.dev/reference/fp/pipe.md).

```typescript
const result = pipe(array, keyBy(getKey));
```

::: info

Prefer the original es-toolkit [`keyBy`](https://massaman.dev/reference/array/keyBy.md) in ordinary code. Use this `fp` variant when composing transformations with [`pipe`](https://massaman.dev/reference/fp/pipe.md).

:::

## Usage

`keyBy` calls `getKey` for each value in the piped array and returns an object whose keys are the returned keys and whose values are the original items. Later values overwrite earlier values with the same key.

```typescript
import { keyBy, pipe } from 'es-toolkit/fp';

pipe(
  [
    { id: 'a', value: 1 },
    { id: 'b', value: 2 },
  ],
  keyBy(item => item.id)
); // => { a: { id: 'a', value: 1 }, b: { id: 'b', value: 2 } }
```

#### Parameters


getKey(item: T) => Krequired

The function that returns the key for each value.

#### Returns

A function that maps a `readonly T[]` to an object keyed by `getKey`.

```typescript
(array: readonly T[]) => Record<K, T>
```


Source: es-toolkit

Re-exported verbatim from [`es-toolkit`](https://es-toolkit.dev/fp/reference/keyBy.html). Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.
