Compute the difference between two JavaScript arrays

Assume we have two JavaScript arrays, alpha and bravo. We need to determine which items appear in alpha, but not in bravo.

We can achieve this by combining the Array.prototype.filter method with the Array.prototype.includes method.

const alpha = ['a', 'b', 'c']
const bravo = ['b', 'c', 'd']

// ['a']
const charlie = alpha.filter(item => !bravo.includes(item))

Sign up for my newsletter

A monthly round-up of blog posts, projects, and internet oddments.