web-dev-qa-db-ja.com

nodejsでlodash / underscoreを使用して2つの配列の違いを見つける方法

配列の配列が2つあり、違いを見つけようとしています。

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も試しました:

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

しかし、それも機能しません。

ここで何が間違っていますか?

16
Firdous Alam

つかいます _.differenceWith、次のように2つの配列を比較するコンパレータを渡します。

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

@ dsl101で述べたように、

_.xor([1, 2, 3], [2, 3, 4]);
// [1, 4]
11
KFunk