es-toolkit/docs/reference/array/unionBy.md
Minsoo Kim cf65b2c601
style(*): Setup prettier and apply formatting (#24)
* chore: add prettierrc

* chore: apply format with prettier config

* chore: eslint error fix
2024-06-04 17:19:26 +09:00

718 B

unionBy

Creates an array of unique values, in order, from all given arrays using a provided mapping function to determine equality.

Signature

function unionBy<T, U>(arr1: T[], arr2: T[], mapper: (item: T) => U): T[];

Parameters

  • 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

unionBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], x => x.id);
// Returns [{ id: 1 }, { id: 2 }, { id: 3 }]