Skip to content
the poor coder

find intersection between two object arrays javascript

Poor Man
Published
Reading time
3 min read
On this page

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:

Text
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:

Text
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:

Text
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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC