Function xorWith

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

    Type Parameters

    • T

      Type of elements in the input arrays.

    Parameters

    • arr1: readonly T[]

      The first array.

    • arr2: readonly T[]

      The second array.

    • areElementsEqual: ((item1: T, item2: T) => boolean)

      The custom equality function to compare elements.

        • (item1, item2): boolean
        • Parameters

          • item1: T
          • item2: T

          Returns boolean

    Returns T[]

    An array containing the elements that are present in either arr1 or arr2 but not in both, based on the custom equality function.

    // Custom equality function for objects with an 'id' property
    const areObjectsEqual = (a, b) => a.id === b.id;
    xorWith([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], areObjectsEqual);
    // Returns [{ id: 1 }, { id: 3 }]