js difference between two arrays of objects

JavaScript: Differences Between Two Arrays of Objects

If you have two arrays of objects in JavaScript and want to find the differences between them, you can use the built-in filter() method to compare the arrays and return any unique values.

Solution 1: Using filter() Method

The filter() method creates a new array with all elements that pass the test provided by a function. In this case, we can use it to compare each object in the first array with every object in the second array, and return only the objects that are unique to the first array.


let array1 = [
  { name: 'John', age: 25 },
  { name: 'Mary', age: 30 },
  { name: 'Bob', age: 20 },
];

let array2 = [
  { name: 'John', age: 25 },
  { name: 'Mary', age: 30 },
];

let diff = array1.filter(x => !array2.some(y => y.name === x.name && y.age === x.age));

console.log(diff); // [{ name: 'Bob', age: 20 }]

In this example, we compare each object in array1 with every object in array2 using the some() method. The some() method tests whether at least one element in the array passes the test provided by a function. If any element in array2 has the same name and age properties as an element in array1, it returns true. The filter() method then returns only the objects in array1 that have no match in array2.

Solution 2: Using lodash differenceBy() Method

If you prefer to use a library like lodash, you can use the differenceBy() method to compare two arrays of objects and return any unique values. This method works similarly to the filter() method, but it allows you to specify the property to compare the objects by.


const _ = require('lodash');

let array1 = [
  { name: 'John', age: 25 },
  { name: 'Mary', age: 30 },
  { name: 'Bob', age: 20 },
];

let array2 = [
  { name: 'John', age: 25 },
  { name: 'Mary', age: 30 },
];

let diff = _.differenceBy(array1, array2, 'name');

console.log(diff); // [{ name: 'Bob', age: 20 }]

In this example, we use the differenceBy() method from lodash to compare each object in array1 with every object in array2 using the name property. The method returns only the objects in array1 that have no match in array2.

Solution 3: Using for..of Loop

If you prefer a more traditional approach, you can use a for..of loop to iterate over the first array and check if each object exists in the second array.


let array1 = [
  { name: 'John', age: 25 },
  { name: 'Mary', age: 30 },
  { name: 'Bob', age: 20 },
];

let array2 = [
  { name: 'John', age: 25 },
  { name: 'Mary', age: 30 },
];

let diff = [];

for (let obj1 of array1) {
  let exists = false;
  
  for (let obj2 of array2) {
    if (obj1.name === obj2.name && obj1.age === obj2.age) {
      exists = true;
      break;
    }
  }
  
  if (!exists) {
    diff.push(obj1);
  }
}

console.log(diff); // [{ name: 'Bob', age: 20 }]

In this example, we use two nested for..of loops to iterate over each object in both arrays and compare their properties. The outer loop iterates over array1, and the inner loop iterates over array2. If an object in array1 has the same name and age properties as an object in array2, we set a flag to true. If the flag is still false after iterating over all objects in array2, we push the object to the diff array.

These are some of the ways to find the differences between two arrays of objects in JavaScript. Choose the method that works best for your use case based on readability, efficiency, and personal preference.

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