xor()
xor<
T>(arrays):T[]
Creates an array of unique values that is the symmetric difference of the given arrays.
Returns values from all arrays that appear in exactly one array.
Type Parametersβ
T: Tβ
The type of elements in the arrays.
Parametersβ
arrays: readonly (readonly T[])[]β
The arrays to inspect.
Returns: T[]β
A new array of filtered values.
Sinceβ
2.0.0
Performanceβ
O(n) β uses Map for counting occurrences across arrays.
Also known asβ
symmetricDifference (Remeda, Ramda) Β· xor (Lodash, es-toolkit) Β· β (Radashi, Effect, Modern Dash, Antfu)
Exampleβ
xor([[2, 1], [2, 3]]);
// => [1, 3]
xor([[1, 2, 3], [3, 4, 5], [5, 6]]);
// => [1, 2, 4, 6]
How it works?β
Symmetric difference β returns values that appear in exactly one array. Values present in multiple arrays are excluded.
Count occurrencesβ
xor vs difference vs intersectionβ
| Function | Returns |
|---|---|
difference(a, b) | In A but not B |
intersection(a, b) | In both A and B |
xor([a, b]) | In A or B, but not both |
Use Casesβ
Find items unique to each array πβ
Get elements that exist in one array but not both (symmetric difference). Perfect for comparing lists and finding discrepancies.
const listA = ["apple", "banana", "cherry"];
const listB = ["banana", "cherry", "date"];
const uniqueToEach = xor([listA, listB]);
// => ["apple", "date"]
Detect changes between snapshotsβ
Find what was added or removed between two states. Useful for tracking modifications or generating changelogs.
const before = ["user1", "user2", "user3"];
const after = ["user2", "user3", "user4"];
const changes = xor([before, after]);
// => ["user1", "user4"] (user1 removed, user4 added)
Compare feature setsβ
Find features unique to each plan or version. Essential for plan comparison or feature matrices.
const basicPlan = ["storage", "email", "support"];
const proPlan = ["storage", "email", "analytics", "api"];
const differences = xor([basicPlan, proPlan]);
// => ["support", "analytics", "api"]
Highlight config drift between environmentsβ
Detect configuration differences between staging and production. Critical for CI/CD pipelines and deployment validation.
const stagingFlags = ["dark-mode", "new-checkout", "beta-search", "ai-chat"];
const productionFlags = ["dark-mode", "new-checkout", "legacy-nav"];
const drift = xor([stagingFlags, productionFlags]);
// => ["beta-search", "ai-chat", "legacy-nav"]
if (drift.length > 0) {
console.warn(`Config drift detected: ${drift.join(", ")}`);
}
Invert selection in a data tableβ
Invert the current selection by XOR-ing with all available items. Essential for "Invert Selection" actions in data tables and file managers.
const allItemIds = ["row-1", "row-2", "row-3", "row-4", "row-5"];
const currentSelection = ["row-1", "row-3"];
// Symmetric difference with the full list = items not currently selected
const inverted = xor([allItemIds, currentSelection]);
// => ["row-2", "row-4", "row-5"]
updateSelection(inverted);
Toggle multiple selections at onceβ
Apply a bulk toggle operation on a selection list. Perfect for "select all in category" or "invert selection" actions.
const currentSelection = ["item-1", "item-3", "item-5"];
const categoryItems = ["item-3", "item-4", "item-5", "item-6"];
// XOR toggles: removes item-3 and item-5 (already selected), adds item-4 and item-6
const newSelection = xor([currentSelection, categoryItems]);
// => ["item-1", "item-4", "item-6"]