[Home] > Snippets  > Languages  > JavaScript  > Arrays  >  Get the intersection of arrays

Get the intersection of arrays

JavaScript

const getIntersection = (a, ...arr) => [...new Set(a)].filter((v) => arr.every((b) => b.includes(v)))

TypeScript

const getIntersection = <T,_>(a: T[], ...arr: T[][]): T[] => [...new Set(a)].filter((v) => arr.every((b) => b.includes(v)))

Examples

getIntersection([1, 2, 3], [2, 3, 4, 5]) // [2, 3]
getIntersection([1, 2, 3], [2, 3, 4, 5], [1, 3, 5]) // [3]