Skip to main content
avoid creating duplicate of the array called a
Source Link
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]a.filter(x => !b1.has(x)))]; 

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

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" ].

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" ].

Source Link
Salvador Dali
  • 224.4k
  • 151
  • 726
  • 766

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" ].