Intersection WithLoading saved progress…

Intersection With

Implement intersectionWith(...arrays, comparator) — return the elements of the first array that have a match in every other array, where "match" is decided by a comparator(a, b) you pass in as the last argument. This mirrors Lodash's _.intersectionWith. The twist that makes it interesting: instead of comparing by a value or a derived key, you compare with an arbitrary two-argument predicate — so you can intersect objects by a field, numbers by a tolerance, or anything else equality can't express directly.

Signature

// ...arrays:   T[][]                    — one or more arrays to intersect.
// comparator:  (a: T, b: T) => boolean  — the LAST argument; returns true when
//                                          two elements should count as equal.
// returns:     T[]
//   The elements of the FIRST array (originals, in order, deduped) that have
//   at least one comparator-match in EVERY other array.
function intersectionWith(...arrays, comparator): T[];

Examples

// Object equality by a field: keep first-array objects whose id appears in the other.
const cmp = (a, b) => a.id === b.id;
intersectionWith([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], cmp);
// → [{ id: 2 }]
// Numeric tolerance: 2.0 is "within 0.5" of 2.4, but 1.0 has no partner.
const close = (a, b) => Math.abs(a - b) < 0.5;
intersectionWith([1.0, 2.0], [2.4, 9], close);
// → [2.0]
// Three arrays: an element must match in ALL of them. Only 2 survives.
const eq = (a, b) => a === b;
intersectionWith([1, 2, 3], [1, 2], [2, 3], eq);
// → [2]

Notes

  • The comparator is the last argument. Everything before it is an array. With intersectionWith(a, b, c, cmp), you intersect a, b, and c; cmp decides equality.
  • Match in every other array, not just one. An element of the first array is kept only if each of the other arrays contains at least one element the comparator approves. A miss in any single array drops it.
  • Return the original first-array elements. When { id: 2 } from the first array matches { id: 2 } from another, you return the first array's object, not the other one — they may differ in their other fields.
  • The result is deduped. Include an element once even if it would match several times, or if it appears twice in the first array. Two first-array elements the comparator treats as equal collapse to one.
  • Order follows the first array. Survivors come out in the order they appear in the first array.
  • Call the comparator as comparator(firstEl, otherEl). The first array's element is always the first argument — it matters for asymmetric comparators.
  • Edge cases. A single array (just the comparator after it) returns the deduped first array. An empty first array, or any later array being empty, makes the result []. Don't mutate the inputs.
Loading editor…