logo
post image

Intersection of 2 Javascript Arrays Made Easy with Sets

The intersection() method of the Set object has made it very easy to get intersection of 2 arrays. This method is supported in all browsers now.

// convert input arrays to Set
const first = new Set([1, 2, 3, 4])
const second = new Set([3, 4, 5, 6])

// convert Set to array
const intersect = [...first.intersection(second)]

// [3, 4]
console.log(intersect)