Performan在两个数组上设置差异运算

时间:2016-07-12 17:33:24

标签: javascript underscore.js

我想执行设置差异操作。 例如。

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 `[]`

1 个答案:

答案 0 :(得分:3)

尝试

   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)));

使用下划线,

_(['a', 'b']).difference(['b', 'c']);
_(['a', 'b']).difference(['a', 'b']);