lodash _.intersection() 最有效的逆是什么

Ste*_*e K 7 javascript lodash

使用lodash,我想找到两个不同数组之间的所有元素,基本上与_.intersection[with|by]相反或相反。当然,我回顾了_.difference[with|by] but it's not an inverse version of _.intersection as it only compares the first array with the second, rather than comparing them against each other. The closest I was able to get is very cludgy which has motivated me to ask here if I'm missing a more efficient and/or elegant option. I'm only interested in lodash based solutions.

这是我能想到的最接近的方法,以获得两个数组之间不同的唯一值数组。id我对具有不在数组中的属性的值以及具有匹配id属性但属性不同的值感兴趣v

const n = [{id: 0, v: 9.7}, {id: 1, v: 1.7}, {id: 3, v: 2.6}, {id: 4, v: 1.89}]
const o = [{id: 1, v: 1.7}, {id: 3, v: 3.6}, {id: 7, v: 0.89}, {id: 4, v: 1.89}]

_.uniqBy(_.concat(
    _.differenceWith(n, o, _.isEqual), _.differenceWith(o, n, _.isEqual)), 'id')
Run Code Online (Sandbox Code Playgroud)

该代码将产生:

[{id: 0, v: 9.7}, {id: 3, v: 2.6}, {id: 7, v: 0.89}]
Run Code Online (Sandbox Code Playgroud)

Sub*_*unk 1

我在寻找相同的东西后发现了这个问题,但我后来意识到,对于我的用例 - 识别不匹配的记录,然后为什么它们不匹配 - 拥有一个列表是没有用的所有的差异都是这样的。

该列表无法告诉我可以使用的任何内容,因此我仍然需要执行进一步的操作来查看哪个数组出现问题。

所以对我来说,首先检查是否相等,然后使用两次更有意义_.difference,因为这样它的结果会产生可用的信息,例如

if (!_.isEqual(a, b) {
  const idsFromBThatAreNotInA = _.difference(a, b);
  const idsFromAThatAreNotInB = _.difference(b, a);
}
Run Code Online (Sandbox Code Playgroud)

我不知道OP的用例是什么,所以我不知道这是否直接相关,但也许它可以帮助其他人。