Skip to content
the poor coder

js difference between two arrays of objects

Poor Man
Published
Reading time
3 min read
On this page

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.

Text

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.

Text

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.

Text

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.

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