admin管理员组文章数量:1303389
Using lodash, I want to find all the elements between two arrays that are different, basically the opposite or inverse of _.intersection[with|by]. Of course I reviewed _.difference[with|by] but it's not an inverse version of _.intersection as it only pares the first array with the second, rather than paring 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.
Here is the closest I could e up with to get a unique array of values that are different between both arrays. I'm interested in values with id
properties that aren't in both arrays and values with matching id
properties but different v
properties.
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')
That code will yield:
[{id: 0, v: 9.7}, {id: 3, v: 2.6}, {id: 7, v: 0.89}]
Using lodash, I want to find all the elements between two arrays that are different, basically the opposite or inverse of _.intersection[with|by]. Of course I reviewed _.difference[with|by] but it's not an inverse version of _.intersection as it only pares the first array with the second, rather than paring 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.
Here is the closest I could e up with to get a unique array of values that are different between both arrays. I'm interested in values with id
properties that aren't in both arrays and values with matching id
properties but different v
properties.
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')
That code will yield:
[{id: 0, v: 9.7}, {id: 3, v: 2.6}, {id: 7, v: 0.89}]
Share
Improve this question
asked Mar 31, 2020 at 4:54
scubastevescubasteve
2,8684 gold badges41 silver badges51 bronze badges
7
-
how did you decided to keep between these two
{id: 3, v: 3.6}
and{id: 3, v: 2.6}
? – Mechanic Commented Mar 31, 2020 at 6:31 -
@CaptainMhmdrz_A the
v
property is different – scubasteve Commented Mar 31, 2020 at 14:47 -
I mean how did you decided to keep
2.6
not3.6
in the output – Mechanic Commented Mar 31, 2020 at 14:49 - Ah, I see. Actually I don't care, I'm only interested in the fact they are different. – scubasteve Commented Mar 31, 2020 at 17:46
- Is it the coronavirus that's responsible for lack of activity or is my question boring or irrelevant? – scubasteve Commented Apr 1, 2020 at 18:58
2 Answers
Reset to default 3_.xorWith is the closest I could think of, but it doesn't work with the example you provided because two objects have an id
of 3.
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}]
_.xorWith(n, o, _.isEqual)
output would be:
[
{ id: 0, v: 9.7 },
{ id: 3, v: 2.6 },
{ id: 3, v: 3.6 },
{ id: 7, v: 0.89 }
]
I found this question after looking for the same thing, but I since came to realize that for my use case - identifying records that don't match, and then why they don't match - it isn't useful to have a list of all differences like that.
That list can't tell me anything I can use, so I would still need to do a further operation to see which array/s are the problem.
So for me, it makes more sense to first check for equality, then to use _.difference
twice, because then the result of it yields usable information, e.g.
if (!_.isEqual(a, b) {
const idsFromBThatAreNotInA = _.difference(a, b);
const idsFromAThatAreNotInB = _.difference(b, a);
}
I don't know what the use case of the OP is, so I don't know if this is directly relevant, but maybe it can help others.
本文标签: javascriptWhat is the most efficient inverse of lodash intersection()Stack Overflow
版权声明:本文标题:javascript - What is the most efficient inverse of lodash _.intersection() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741756423a2396140.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论