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

# unionWith (Functional Programming) es-toolkit

Creates a function that combines arrays using a custom equality function. Use it with [`pipe`](https://massaman.dev/reference/fp/pipe.md).

```typescript
const result = pipe(array, unionWith(secondArray, areItemsEqual));
```

::: info

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

:::

## Usage

`unionWith` keeps the first value from the combined arrays for which `areItemsEqual` does not match an already kept value.

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

pipe(
  [{ id: 1 }, { id: 2 }],
  unionWith([{ id: 2 }, { id: 3 }], (a, b) => a.id === b.id)
); // => [{ id: 1 }, { id: 2 }, { id: 3 }]
```

#### Parameters


secondArrayreadonly T[]required

The array to combine after the piped array.


areItemsEqual(item: T, other: T) => booleanrequired

The function that decides whether two values are equal.

#### Returns

A function that maps a `readonly T[]` to a union by custom equality.

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


Source: es-toolkit

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