intersection()
intersection<
T>(...arrays):T[]
Computes the intersection of multiple arrays.
This function takes multiple arrays and returns a new array containing only the elements that are present in all input arrays.
Type Parametersβ
T: Tβ
The type of the elements in the input arrays.
Parametersβ
arrays: ...readonly (readonly T[])[]β
Arrays of type T to compute the intersection from.
Returns: T[]β
A new array containing the common elements found in all input arrays.
Sinceβ
2.0.0
Performanceβ
O(n Γ m) time & space where n = first array length, m = number of arrays. Uses Set for constant-time lookups.
Also known asβ
intersection (Lodash, es-toolkit, Remeda, Ramda, Effect, Modern Dash) Β· intersects (Radashi) Β· β (Antfu)
Exampleβ
intersection([1, 2, 3], [2, 3, 4], [3, 4, 5]);
// => [3]
intersection(['a', 'b'], ['b', 'c'], ['b', 'd']);
// => ['b']
How it works?β
Returns elements that exist in ALL input arrays. Uses Set for O(1) membership testing.
Use Casesβ
Find common permissions across user roles πβ
Identify shared access rights between multiple roles or user groups. Perfect for permission systems, access control, or role-based authorization.
const adminPermissions = ["read", "write", "delete", "admin", "audit"];
const editorPermissions = ["read", "write", "publish", "audit"];
const viewerPermissions = ["read", "comment", "audit"];
const commonPermissions = intersection(
adminPermissions,
editorPermissions,
viewerPermissions
);
// => ["read", "audit"]
Filter available time slots for schedulingβ
Find overlapping availability across multiple participants for meetings. Ideal for calendar apps, booking systems, or team scheduling tools.
const aliceSlots = ["09:00", "10:00", "11:00", "14:00", "15:00"];
const bobSlots = ["10:00", "11:00", "13:00", "14:00", "16:00"];
const charlieSlots = ["08:00", "10:00", "14:00", "15:00", "17:00"];
const availableForAll = intersection(aliceSlots, bobSlots, charlieSlots);
// => ["10:00", "14:00"]
Identify common tags across itemsβ
Find shared attributes between products, articles, or any tagged entities. Useful for recommendation engines, filtering systems, or content analysis.
const article1Tags = ["javascript", "typescript", "react", "frontend"];
const article2Tags = ["typescript", "nodejs", "backend", "react"];
const article3Tags = ["react", "typescript", "testing", "jest"];
const commonTags = intersection(article1Tags, article2Tags, article3Tags);
// => ["typescript", "react"]
Resolve active feature flags for a userβ
Find which features are enabled for a user by intersecting their segments with active flags. Essential for feature flag systems, gradual rollouts, and A/B testing platforms.
const userSegments = ["beta-testers", "premium", "eu-region"];
const betaFeatures = ["new-dashboard", "ai-search"];
const premiumFeatures = ["ai-search", "export-pdf", "priority-support"];
const euFeatures = ["gdpr-banner", "cookie-consent", "ai-search"];
// Features available to ALL of the user's segments
const universalFeatures = intersection(betaFeatures, premiumFeatures, euFeatures);
// => ["ai-search"]
Find compatible filter options in faceted searchβ
Determine which filter values remain valid when multiple filters are combined. Critical for e-commerce product filtering where options narrow down as filters are applied.
// Products available in size "M"
const sizeMColors = ["red", "blue", "green", "black"];
// Products available in brand "Nike"
const nikeColors = ["blue", "black", "white"];
// Products available under $50
const budgetColors = ["red", "blue", "black", "yellow"];
// Colors available for: size M + Nike + under $50
const availableColors = intersection(sizeMColors, nikeColors, budgetColors);
// => ["blue", "black"]
Find common skills for team matchingβ
Identify shared skills between candidates and job requirements. Perfect for recruitment platforms and team composition tools.
const jobRequirements = ["typescript", "react", "graphql", "testing"];
const candidateSkills = ["typescript", "react", "vue", "nodejs", "testing"];
const matchingSkills = intersection(jobRequirements, candidateSkills);
// => ["typescript", "react", "testing"]
const matchRate = matchingSkills.length / jobRequirements.length;
// => 0.75 (75% match)