> 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.

# groupBy (Functional Programming) es-toolkit

Creates a function that groups values by a key. Use it with [`pipe`](https://massaman.dev/reference/fp/pipe.md).

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

::: info

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

:::

## Usage

`groupBy` calls `getKey` for each value in the piped array and returns an object whose keys are the returned keys and whose values are arrays of matching items.

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

pipe(
  ['one', 'two', 'three'],
  groupBy(word => word.length)
); // => { 3: ['one', 'two'], 5: ['three'] }
```

#### Parameters


getKey(item: T) => Krequired

The function that returns the group key for each value.

#### Returns

A function that maps a `readonly T[]` to grouped arrays by key.

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


Source: es-toolkit

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