find intersection between two object arrays javascript

How to Find Intersection Between Two Object Arrays in JavaScript

As a web developer, I have encountered the need to find the intersection between two object arrays many times. In JavaScript, there are several ways to achieve this. Here are some of them:

Method 1: Using filter() and includes()

The first method involves using the filter() method and the includes() method to find the intersection between two object arrays. Here is the code:

const array1 = [{id: 1, name: 'John'}, {id: 2, name: 'Jane'}, {id: 3, name: 'Mike'}];
const array2 = [{id: 2, name: 'Jane'}, {id: 4, name: 'Mary'}];

const intersection = array1.filter(obj1 => array2.some(obj2 => obj2.id === obj1.id));

console.log(intersection); // Output: [{id: 2, name: 'Jane'}]

In this code, we first define two object arrays array1 and array2. We then use the filter() method on array1, passing a callback function that checks if any object in array2 has the same id as the current object in array1. We use the some() method to accomplish this. Finally, we store the resulting array in a variable called intersection.

Method 2: Using reduce() and filter()

The second method involves using the reduce() method and the filter() method to find the intersection between two object arrays. Here is the code:

const array1 = [{id: 1, name: 'John'}, {id: 2, name: 'Jane'}, {id: 3, name: 'Mike'}];
const array2 = [{id: 2, name: 'Jane'}, {id: 4, name: 'Mary'}];

const intersection = array1.reduce((acc, obj1) => {
  const found = array2.find(obj2 => obj2.id === obj1.id);
  if (found) {
    acc.push(found);
  }
  return acc;
}, []);

console.log(intersection); // Output: [{id: 2, name: 'Jane'}]

In this code, we first define two object arrays array1 and array2. We then use the reduce() method on array1, passing a callback function that checks if any object in array2 has the same id as the current object in array1. If a match is found, we push the object to an accumulator array. Finally, we store the resulting array in a variable called intersection.

Method 3: Using lodash library

The third method involves using the lodash library. Lodash is a JavaScript utility library that provides many useful functions for working with arrays, objects, and other data types. Here is the code:

const _ = require('lodash');

const array1 = [{id: 1, name: 'John'}, {id: 2, name: 'Jane'}, {id: 3, name: 'Mike'}];
const array2 = [{id: 2, name: 'Jane'}, {id: 4, name: 'Mary'}];

const intersection = _.intersectionBy(array1, array2, 'id');

console.log(intersection); // Output: [{id: 2, name: 'Jane'}]

In this code, we first import the lodash library using the require() function. We then define two object arrays array1 and array2. We use the _.intersectionBy() function from lodash to find the intersection between the two arrays based on the id property. Finally, we store the resulting array in a variable called intersection.

These are just some of the methods you can use to find the intersection between two object arrays in JavaScript. Choose the one that best fits your needs and preferences.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe