Skip to main content
1 of 2
ifelse.codes
  • 2.4k
  • 25
  • 21

A cleaner approach in ES6 is the following solution.

var a1 = ['a', 'b']; var a2 = ['a', 'b', 'c', 'd']; 

Difference

a2.filter(d => !a1.includes(d)) // gives ["c", "d"] 

Intersection

a2.filter(d => a1.includes(d)) // gives ["a", "b"] 
ifelse.codes
  • 2.4k
  • 25
  • 21