VDL Plugin SDK

xorBy

Import from @varavel/vdl-plugin-sdk/utils/arrays.

function xorBy<T, U>(
   arr1, 
   arr2, 
   mapper
): T[];

Computes the symmetric difference between two arrays using a custom mapping function. The symmetric difference is the set of elements which are in either of the arrays, but not in their intersection, determined by the result of the mapping function.

Type Parameters

Type ParameterDescription
TType of elements in the input arrays.
UType of the values returned by the mapping function.

Parameters

ParameterTypeDescription
arr1readonly T[]The first array.
arr2readonly T[]The second array.
mapper(item) => UThe function to map array elements to comparison values.

Returns

T[]

An array containing the elements that are present in either arr1 or arr2 but not in both, based on the values returned by the mapping function.

Example

// Custom mapping function for objects with an 'id' property
const idMapper = obj => obj.id;
xorBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper);
// Returns [{ id: 1 }, { id: 3 }]