-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
  • 30,526
  • 12
  • 76
  • 90
newBike
  • 13,728
  • 26
  • 98
  • 169

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,330
  • 13
  • 47
  • 70