Open In App

How to do a Deep Comparison Between Two Objects using Lodash?

Last Updated : 23 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Deep Comparison between two objects is the process of thoroughly checking their properties and nested objects to determine if they are equal.

Below are the possible approaches to do a deep comparison between two objects using Lodash.:

Run the below command to install Loadash JavaScript Library:

npm install loadash

Using _.isEqual() Function

In this approach, we are using Lodash's _.isEqual() function to perform a deep comparison between two objects, obj1, and obj2, checking if their properties and nested arrays are exactly equal. This method returns true if both objects have the same structure and values for all keys and arrays.

Syntax:

_.isEqual(object, other);

Example: The below example uses the _.isEqual() function to do a deep comparison between two objects using Lodash.

JavaScript
const _ = require('lodash');
const obj1 = {
    course: 'Data Structures and Algorithms in Python',
    duration: '6 Weeks',
    instructor: 'GFG',
    topics: ['Arrays', 'Linked Lists', 'Stacks', 'Queues']
};

const obj2 = {
    course: 'Data Structures and Algorithms in Python',
    duration: '6 Weeks',
    instructor: 'GFG',
    topics: ['Arrays', 'Linked Lists', 'Stacks', 'Queues']
};
const isEqual = _.isEqual(obj1, obj2);
console.log('Objects are equal:', isEqual);

Output:

Objects are equal: true

Using _.isEqualWith() Method with Comparator Function

In this approach, we are using the _.isEqualWith() method from Lodash along with a custom comparator function to perform a deep comparison between two objects. The comparator function checks if the values are arrays and compares them after sorting to handle order differences.

Syntax:

_.isEqualWith(value, other, [customizer]);

Example: The below example uses the _.isEqualWith() Method with Comparator Function to do a deep comparison between two objects using Lodash.

JavaScript
const _ = require('lodash');
const obj1 = {
    course: 'Data Structures and Algorithms in Python',
    duration: '6 Weeks',
    instructor: 'GFG',
    topics: ['Arrays', 'Linked Lists', 'Stacks', 'Queues']
};
const obj2 = {
    course: 'Data Structures and Algorithms in Python',
    duration: '6 Weeks',
    instructor: 'GFG',
    topics: ['Arrays', 'Linked Lists', 'Stacks', 'Queues']
};
const comparator = (value1, value2) => {
    if (_.isArray(value1) && _.isArray(value2)) {
        return _.isEqual(_.sortBy(value1), _.sortBy(value2));
    }
    return undefined;
};
const res = _.isEqualWith(obj1, obj2, comparator);
console.log('Objects are equal:', res);

Output:

Objects are equal: true

Using _.differenceWith() for Array Comparison

In this approach, we use Lodash's _.differenceWith() function to compare arrays of objects. This method checks for differences between two arrays of objects using a custom comparator function.

Syntax:

_.differenceWith(array, [values], [comparator]);

Example:

JavaScript
const _ = require('lodash');

const arr1 = [
    { id: 1, name: 'Nikunj' },
    { id: 2, name: 'Dhruv' }
];

const arr2 = [
    { id: 1, name: 'Nikunj' },
    { id: 2, name: 'Dhruv' }
];

const comparator = (obj1, obj2) => _.isEqual(obj1, obj2);

const differences = _.differenceWith(arr1, arr2, comparator);
console.log('Differences:', differences); // Output: []

const arr3 = [
    { id: 1, name: 'Nikunj' },
    { id: 3, name: 'Yash' }
];

const differences2 = _.differenceWith(arr1, arr3, comparator);
console.log('Differences:', differences2); // Output: [{ id: 2, name: 'Dhruv' }]

Output:

[{ id: 2, name: 'Dhruv' }]

Using _.transform() for Custom Deep Comparison

In this approach, we use Lodash’s _.transform() function to perform a custom deep comparison between two objects. This method allows us to compare each property of the objects recursively and handle differences according to custom logic.

Syntax:

_.transform(object, [iteratee], [accumulator]);

Example: The below example uses the _.transform() function to do a deep comparison between two objects using Lodash.

JavaScript
const _ = require('lodash');

const obj1 = {
    course: 'Data Structures and Algorithms in Python',
    duration: '6 Weeks',
    instructor: 'GFG',
    topics: ['Arrays', 'Linked Lists', 'Stacks', 'Queues'],
    person: 'Nikunj'
};

const obj2 = {
    course: 'Data Structures and Algorithms in Python',
    duration: '6 Weeks',
    instructor: 'GFG',
    topics: ['Arrays', 'Linked Lists', 'Stacks', 'Queues'],
    person: 'Dhruv'
};

const customDeepComparison = (obj1, obj2) => {
    return _.transform(obj1, (result, value, key) => {
        if (!_.isEqual(value, obj2[key])) {
            result[key] = value;
        }
    }, {});
};

const differences = customDeepComparison(obj1, obj2);
const isEqual = _.isEmpty(differences);

console.log('Objects are equal:', isEqual); 
console.log('Differences:', differences); 

Output:

Objects are equal: false
Differences: { person: 'Nikunj' }

Next Article

Similar Reads