Skip to main content
2 of 2
avoid creating duplicate of the array called a
Magne
  • 17.4k
  • 11
  • 75
  • 97

With the arrival of ES6 with sets and splat operator (at the time of being works only in Firefox, check compatibility table), you can write the following one liner:

var a = ['a', 'b', 'c', 'd']; var b = ['a', 'b']; var b1 = new Set(b); var difference = [...new Set(a.filter(x => !b1.has(x)))]; 

which will result in [ "c", "d" ].

Salvador Dali
  • 224.4k
  • 151
  • 726
  • 766