javascript intersect two object arrays

JavaScript: Intersect Two Object Arrays

Have you ever needed to find the common objects between two arrays of objects in JavaScript? This is a common problem when dealing with data manipulation and analysis.

Method 1: Using the filter() method and includes()

One way to solve this problem is to use the filter() method along with the includes() method. Here's how:


const array1 = [{name: 'John', age: 20}, {name: 'Jane', age: 25}, {name: 'Bob', age: 30}];
const array2 = [{name: 'Jane', age: 25}, {name: 'Bob', age: 30}, {name: 'Joe', age: 35}];

const result = array1.filter(obj1 => {
  return array2.some(obj2 => {
    return obj1.name === obj2.name
  });
});

console.log(result); // [{name: 'Jane', age: 25}, {name: 'Bob', age: 30}]

In the above code, we first declare two arrays of objects, array1 and array2. We then use the filter() method on array1. The filter() method takes a function as an argument and returns a new array with all the elements that pass the condition of the function.

In our case, the function takes an object obj1 from array1. It then checks if array2 contains an object with the same name property as obj1 using the some() method. If it finds a matching object, it returns true, and the object obj1 is included in the new array result.

Method 2: Using the reduce() method

Another way to find the common objects between two arrays of objects is to use the reduce() method. Here's how:


const array1 = [{name: 'John', age: 20}, {name: 'Jane', age: 25}, {name: 'Bob', age: 30}];
const array2 = [{name: 'Jane', age: 25}, {name: 'Bob', age: 30}, {name: 'Joe', age: 35}];

const result = array1.reduce((accumulator, obj1) => {
  const found = array2.find(obj2 => obj1.name === obj2.name);
  if (found) {
    accumulator.push(found);
  }
  return accumulator;
}, []);

console.log(result); // [{name: 'Jane', age: 25}, {name: 'Bob', age: 30}]

In the above code, we use the reduce() method on array1. The reduce() method takes a function as an argument and returns a single value that is the accumulated result of calling the function on each element of the array.

In our case, the function takes two arguments: an accumulator and an object obj1 from array1. It then uses the find() method on array2 to check if there is an object with the same name property as obj1. If it finds a matching object, it pushes it to the accumulator array. At the end, the function returns the accumulator array, which contains all the common objects between the two arrays.

Conclusion

There are several ways to find the common objects between two arrays of objects in JavaScript. The two methods we discussed above are just a few examples. Depending on your specific use case, there might be other methods that are more suitable. However, with these two methods, you should have a good starting point to solve this common problem.

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