intersectionWith()
intersectionWith<
T>(arrays,comparator):T[]
Creates an array of unique values present in all arrays, using a comparator for equality.
Type Parametersโ
T: Tโ
The type of elements in the arrays.
Parametersโ
arrays: readonly (readonly T[])[]โ
An array of arrays to inspect.
comparator: (a, b) => booleanโ
A function that returns true if two elements are equivalent.
Returns: T[]โ
A new array containing elements present in all input arrays.
Sinceโ
2.0.0
Performanceโ
O(nยฒ ร m) โ custom comparators cannot leverage Set optimization.
Also known asโ
innerJoin (Ramda) ยท intersectionWith (Lodash, es-toolkit, Remeda, Effect) ยท โ (Radashi, Modern Dash, Antfu)
Exampleโ
intersectionWith(
[[{ x: 1 }, { x: 2 }], [{ x: 2 }, { x: 3 }]],
(a, b) => a.x === b.x
);
// => [{ x: 2 }]
intersectionWith(
[[1.1, 2.2], [2.3, 3.1]],
(a, b) => Math.floor(a) === Math.floor(b)
);
// => [2.2]
How it works?โ
Compares elements using a custom comparator function.
Use Casesโ
Find matching coordinates across map datasets ๐โ
Identify common locations with tolerance for floating-point precision. Perfect for GPS data, geospatial analysis, or location-based services.
const sensorAReadings = [
{ lat: 48.8566, lng: 2.3522, temp: 22 },
{ lat: 51.5074, lng: -0.1278, temp: 18 },
{ lat: 40.7128, lng: -74.006, temp: 25 },
];
const sensorBReadings = [
{ lat: 48.8567, lng: 2.3523, temp: 21 }, // ~same as Paris
{ lat: 35.6762, lng: 139.6503, temp: 28 },
{ lat: 40.7127, lng: -74.0059, temp: 24 }, // ~same as NYC
];
const TOLERANCE = 0.001;
const commonLocations = intersectionWith(
[sensorAReadings, sensorBReadings],
(a, b) =>
Math.abs(a.lat - b.lat) < TOLERANCE && Math.abs(a.lng - b.lng) < TOLERANCE
);
// => [Paris, NYC locations]
Match objects with deep equality comparisonโ
Find identical complex objects across multiple collections. Ideal for state comparison, cache validation, or test assertions.
const configSetA = [
{ feature: "auth", settings: { provider: "oauth", timeout: 3000 } },
{ feature: "cache", settings: { ttl: 600, maxSize: 100 } },
];
const configSetB = [
{ feature: "cache", settings: { ttl: 600, maxSize: 100 } },
{ feature: "auth", settings: { provider: "oauth", timeout: 5000 } },
];
const deepEqual = (a, b) =>
a.feature === b.feature &&
JSON.stringify(a.settings) === JSON.stringify(b.settings);
const identicalConfigs = intersectionWith([configSetA, configSetB], deepEqual);
// => [{ feature: "cache", settings: { ttl: 600, maxSize: 100 } }]
Find overlapping time ranges across schedulesโ
Detect common availability windows using custom range overlap logic. Useful for booking systems, resource allocation, or shift planning.
interface TimeSlot {
start: number;
end: number;
label: string;
}
const roomASlots: TimeSlot[] = [
{ start: 9, end: 12, label: "Morning Block" },
{ start: 14, end: 17, label: "Afternoon" },
];
const roomBSlots: TimeSlot[] = [
{ start: 10, end: 13, label: "Mid-Morning" },
{ start: 15, end: 18, label: "Late Afternoon" },
];
const hasOverlap = (a: TimeSlot, b: TimeSlot): boolean => {
const overlapStart = Math.max(a.start, b.start);
const overlapEnd = Math.min(a.end, b.end);
return overlapEnd - overlapStart >= 1;
};
const overlappingSlots = intersectionWith(
[roomASlots, roomBSlots],
hasOverlap
);
// => Slots with at least 1 hour overlap