It's All Writing.

Writing makes you happy.

How to get arrays intersection include duplicate values at node

Prerequisites

There are two arrays has multiple elements as below.

array1 is

[
  {
    "mail": "shinchi@its-all-writing.com",
    "hobby": "Walking"
  },
  {
    "mail": "shinchi@its-all-writing.com",
    "hobby": "Playing the Bamboo Flute"
  },
  {
    "mail": "kazuyo@its-all-writing.com",
    "hobby": "Reading Books"
  },
  {
    "mail": "foo@its-all-writing.com",
    "hobby": "Driving"
  }
]

array2 is

[
  {
    "mail": "shinchi@its-all-writing.com"
  },
  {
    "mail": "kazuyo@its-all-writing.com"
  },
  {
    "mail": "bar@its-all-writing.com"
  }
]

Then, we want to get this array to be as below.

[
  {
    "mail": "shinchi@its-all-writing.com",
    "hobby": "Walking"
  },
  {
    "mail": "shinchi@its-all-writing.com",
    "hobby": "Playing the Bamboo Flute"
  },
  {
    "mail": "kazuyo@its-all-writing.com",
    "hobby": "Reading Books"
  }
]

lodash provides intersectionBy function. But, if you use it, it returns intersection includes unique values ( is not duplicate ). In this case, intersectionBy returns array to be as below.

const lodash = require('lodash');

let array1 = [
  {
    "mail": "shinchi@its-all-writing.com",
    "hobby": "Walking"
  },
  {
    "mail": "shinchi@its-all-writing.com",
    "hobby": "Playing the Bamboo Flute"
  },
  {
    "mail": "kazuyo@its-all-writing.com",
    "hobby": "Reading Books"
  },
  {
    "mail": "foo@its-all-writing.com",
    "hobby": "Driving"
  }
];

let array2 = [
  {
    "mail": "shinchi@its-all-writing.com"
  },
  {
    "mail": "kazuyo@its-all-writing.com"
  },
  {
    "mail": "bar@its-all-writing.com"
  }
];

let result = lodash.intersectionBy(array1, array2, 'mail');
// =>
// [
//   {
//     "mail": "shinchi@its-all-writing.com",
//     "hobby": "Walking"
//   },
//   {
//     "mail": "kazuyo@its-all-writing.com",
//     "hobby": "Reading Books"
//   }
// ]

Enviroment

  • node

Code

let array1 = [
  {
    "mail": "shinchi@its-all-writing.com",
    "hobby": "Walking"
  },
  {
    "mail": "shinchi@its-all-writing.com",
    "hobby": "Playing the Bamboo Flute"
  },
  {
    "mail": "kazuyo@its-all-writing.com",
    "hobby": "Reading Books"
  },
  {
    "mail": "foo@its-all-writing.com",
    "hobby": "Driving"
  }
];

let array2 = [
  {
    "mail": "shinchi@its-all-writing.com"
  },
  {
    "mail": "kazuyo@its-all-writing.com"
  },
  {
    "mail": "bar@its-all-writing.com"
  }
];

let intersectionWith = function(f, xs, ys) {
  return xs.filter(function(x) {
    return ys.some(function(y) {
      return f(x, y);
    });
  });
};

let equals = function(x,y) {
  return x.mail === y.mail;
};

let result = intersectionWith(equals, array1, array2);
// =>
// [
//   {
//     "mail": "shinchi@its-all-writing.com",
//     "hobby": "Walking"
//   },
//   {
//     "mail": "shinchi@its-all-writing.com",
//     "hobby": "Playing the Bamboo Flute"
//   },
//   {
//     "mail": "kazuyo@its-all-writing.com",
//     "hobby": "Reading Books"
//   }
// ]

Reference