mirror of
https://github.com/toss/es-toolkit.git
synced 2024-12-18 05:21:43 +03:00
cf65b2c601
* chore: add prettierrc * chore: apply format with prettier config * chore: eslint error fix
718 B
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 }]