Javascript 如何在nodejs中使用lodash/underscore找出两个数组之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38865869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to find difference between two array using lodash/underscore in nodejs
提问by Firdous Alam
I have two arrays of arrays and am trying to find the difference.
我有两个数组数组,并试图找到差异。
var a = [[ 11, 24, 28, 38, 42, 44 ],
[ 7, 19, 21, 22, 29, 38 ],
[ 2, 21, 27, 30, 33, 40 ],
[ 6, 11, 12, 21, 34, 48 ],
[ 1, 10, 17, 31, 35, 40 ],
[ 1, 18, 26, 33, 36, 45 ],
[ 15, 21, 22, 24, 38, 46 ],
[ 5, 17, 21, 27, 29, 41 ],
[ 3, 7, 12, 16, 20, 28 ],
[ 9, 12, 13, 18, 30, 37 ],
[ 3, 19, 21, 31, 33, 46 ],
[ 6, 11, 16, 18, 20, 34 ],
[ 1, 3, 11, 13, 24, 28 ],
[ 12, 13, 16, 40, 42, 46 ],
[ 1, 3, 5, 36, 37, 41 ],
[ 14, 15, 23, 24, 26, 31 ],
[ 7, 13, 14, 15, 27, 28 ]];
var b = [[ 4, 7, 9, 21, 31, 36 ],
[ 2, 5, 6, 12, 15, 21 ],
[ 4, 7, 8, 15, 38, 41 ],
[ 11, 24, 28, 38, 42, 44 ],
[ 7, 19, 21, 22, 29, 38 ]];
How would I find:
我将如何找到:
c = [[ 2, 21, 27, 30, 33, 40 ],
[ 6, 11, 12, 21, 34, 48 ],
[ 1, 10, 17, 31, 35, 40 ],
[ 1, 18, 26, 33, 36, 45 ],
[ 15, 21, 22, 24, 38, 46 ],
[ 5, 17, 21, 27, 29, 41 ],
[ 3, 7, 12, 16, 20, 28 ],
[ 9, 12, 13, 18, 30, 37 ],
[ 3, 19, 21, 31, 33, 46 ],
[ 6, 11, 16, 18, 20, 34 ],
[ 1, 3, 11, 13, 24, 28 ],
[ 12, 13, 16, 40, 42, 46 ],
[ 1, 3, 5, 36, 37, 41 ],
[ 14, 15, 23, 24, 26, 31 ],
[ 7, 13, 14, 15, 27, 28 ]];
I had tried underscore:
我试过下划线:
_ = require('underscore');
_.difference(a,b);
But it doesn't work.
但它不起作用。
I also tried lodash:
我也试过 lodash:
_ = require('lodash');
_.differenceBy(a,b);
but it doesn't work either.
但它也不起作用。
What am I doing wrong here?
我在这里做错了什么?
回答by
Use _.differenceWith
, and pass a comparator which compares two arrays, as in:
使用_.differenceWith
, 并传递一个比较两个数组的比较器,如下所示:
_.differenceWith(a, b, _.isEqual);
回答by KFunk
As mentioned by @dsl101,
正如@dsl101 所提到的,
_.xor([1, 2, 3], [2, 3, 4]);
// [1, 4]