如何在nodejs中使用lodash/下划线找到两个数组之间的差异 [英] how to find difference between two array using lodash/underscore in nodejs

查看:347
本文介绍了如何在nodejs中使用lodash/下划线找到两个数组之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组的数组,正在尝试找出差异.

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

我将如何找到:

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

我曾尝试使用下划线:

_ = require('underscore');
_.difference(a,b);

但这是行不通的.

我也尝试过lodash:

I also tried lodash:

_ = require('lodash');
_.differenceBy(a,b);

但它也不起作用.

我在做什么错了?

推荐答案

使用_.differenceWith,并传递一个比较两个数组的比较器,如下所示:

Use _.differenceWith, and pass a comparator which compares two arrays, as in:

_.differenceWith(a, b, _.isEqual);

这篇关于如何在nodejs中使用lodash/下划线找到两个数组之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆