es-toolkit/docs/reference/array/unionBy.md

27 lines
718 B
Markdown
Raw Normal View History

2024-04-25 14:56:13 +03:00
# unionBy
Creates an array of unique values, in order, from all given arrays using a provided mapping function to determine equality.
## Signature
```typescript
function unionBy<T, U>(arr1: T[], arr2: T[], mapper: (item: T) => U): T[];
2024-04-25 14:56:13 +03:00
```
### Parameters
2024-04-25 14:56:13 +03:00
- `arr1` (`T[]`): The first array.
- `arr2` (`U[]`): The second array.
- `mapper`: (`(item: T) => U`): The function to map array elements to comparison values.
### Returns
(`T[]`): A new array containing the union of unique elements from `arr1` and `arr2`, based on the values returned by the mapping function.
## Examples
```typescript
unionBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], x => x.id);
// Returns [{ id: 1 }, { id: 2 }, { id: 3 }]
```