unionWith
Import from @varavel/vdl-plugin-sdk/utils/arrays.
function unionWith<T>(
arr1,
arr2,
areItemsEqual
): T[];
Creates an array of unique values from two given arrays based on a custom equality function.
This function takes two arrays and a custom equality function, merges the arrays, and returns a new array containing only the unique values as determined by the custom equality function.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of elements in the array. |
Parameters
| Parameter | Type | Description |
|---|---|---|
arr1 | readonly T[] | The first array to merge and filter for unique values. |
arr2 | readonly T[] | The second array to merge and filter for unique values. |
areItemsEqual | (item1, item2) => boolean | A custom function to determine if two elements are equal. It takes two arguments and returns true if the elements are considered equal, and false otherwise. |
Returns
T[]
A new array of unique values based on the custom equality function.
Example
const array1 = [{ id: 1 }, { id: 2 }];
const array2 = [{ id: 2 }, { id: 3 }];
const areItemsEqual = (a, b) => a.id === b.id;
const result = unionWith(array1, array2, areItemsEqual);
// result will be [{ id: 1 }, { id: 2 }, { id: 3 }] since { id: 2 } is considered equal in both arrays