-1

I want to perform set difference operation. Eg.

var a = ['a', 'b'], tw = ['b', 'c'], cn = ['a', 'b']
var zz = a - tw // => zz should be `['a']`
var zz = a - cn // => zz should be `[]`
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
newBike
  • 14,385
  • 29
  • 109
  • 192

1 Answers1

3

Try

   let a = new Set(['a', 'b']);
   let tw = new Set(['b', 'c']);
   let cn = new Set(['a', 'b'])

   let diff1 = new Set([...a].filter(x => !tw.has(x)));
   let diff2 = new Set([...a].filter(x => !cn.has(x)));

Using underscore,

_(['a', 'b']).difference(['b', 'c']);
_(['a', 'b']).difference(['a', 'b']);
Hector Barbossa
  • 5,506
  • 13
  • 48
  • 70