Skip to main content

Built and signed on GitHub Actions

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 weeks ago (1.31.0)
function xorWith
xorWith<T>(
arr1: readonly T[],
arr2: readonly T[],
areElementsEqual: (
item1: T,
item2: T,
) => boolean
,
): T[]

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.

Examples

Example 1

// 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 }]

Type Parameters

  • 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.

Return Type

T[]

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

Add Package

deno add jsr:@es-toolkit/es-toolkit

Import symbol

import { xorWith } from "@es-toolkit/es-toolkit/compat";

---- OR ----

Import directly with a jsr specifier

import { xorWith } from "jsr:@es-toolkit/es-toolkit/compat";

Add Package

npx jsr add @es-toolkit/es-toolkit

Import symbol

import { xorWith } from "@es-toolkit/es-toolkit/compat";

Add Package

yarn dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { xorWith } from "@es-toolkit/es-toolkit/compat";

Add Package

pnpm dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { xorWith } from "@es-toolkit/es-toolkit/compat";

Add Package

bunx jsr add @es-toolkit/es-toolkit

Import symbol

import { xorWith } from "@es-toolkit/es-toolkit/compat";