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

# unionBy (Functional Programming) es-toolkit

Creates a function that combines arrays by a mapped key without duplicates. Use it with [`pipe`](https://massaman.dev/reference/fp/pipe.md).

```typescript
const result = pipe(array, unionBy(secondArray, mapper));
```

::: info

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

:::

## Usage

`unionBy` compares the values returned by `mapper`. It keeps the first value for each mapped key from the piped array, then from `secondArray`.

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

pipe(
  [{ id: 1 }, { id: 2 }],
  unionBy([{ id: 2 }, { id: 3 }], item => item.id)
); // => [{ id: 1 }, { id: 2 }, { id: 3 }]
```

#### Parameters


secondArrayreadonly T[]required

The array to combine after the piped array.


mapper(item: T) => Urequired

The function that returns the key used for uniqueness.

#### Returns

A function that maps a `readonly T[]` to a union by mapped key.

```typescript
(array: readonly T[]) => T[]
```


Source: es-toolkit

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