How to Compare Two Objects using Lodash?
Last Updated :
18 Jul, 2024
To compare two objects using lodash, we employ Lodash functions such as _.isEqual(), _.isMatch(), and _.isEqualWith(). These methods enable us to do a comparison between two objects and determine if they are equivalent or not.
Below are the approaches to do a comparison between two objects using Lodash:
Run the below command to install Lodash JavaScript Library:
npm install lodash
Using Lodash _.isEqual() Method
In this approach, we use the _.isEqual() from Lodash to do a comparison between two values to determine if they are equivalent. This method supports comparing arrays, array buffers, boolean, date objects, maps, numbers, objects, regex, sets, strings, symbols, and typed arrays.
Syntax:
_.isEqual( value1, value2);
Example: The below example shows how to do a comparison between 2 objects with lodash using _the .isEqual() Method.
JavaScript
// Defining Lodash variable
const _ = require('lodash');
// First object
const obj1 = { a: 1, b: { c: 2, d: [3, 4] } };
// Second object
const obj2 = { a: 1, b: { c: 2, d: [3, 4] } };
// Checking for Equal Value
const isEqual = _.isEqual(obj1, obj2);
console.log(isEqual);
Output:
true
Using Lodash _.isMatch() Method
In this approach, we are using the Lodash _.isMatch() method to perform a partial comparison between other object and sources to determine if the object contains equivalent property values.
Syntax:
_.isMatch(object, source);
Example: The below example shows how to do a comparison between 2 objects with lodash using the _.isMatch() method.
JavaScript
// Defining Lodash variable
const _ = require('lodash');
// First object
const obj = { a: 1, b: { c: 2, d: [3, 4] } };
// Second object
const source = { b: { c: 2 } };
// Doing a deep comparison
const isMatch = _.isMatch(obj, source);
console.log(isMatch);
Output:
true
Using _.isEqualWith() Method
In this approach, we are using the _.isEqualWith() method to do a deep comparison between two values to determine if they are equivalent. Also, it accepts a customizer which is called to compare values. Moreover, if the customizer used here returns undefined then the comparisons are dealt with by the method instead.
Syntax:
_.isEqualWith(value, other, [customizer]);
Example: The below example shows how to do a deep comparison between 2 objects with lodash using the isEqualWith() method.
JavaScript
// Import the lodash library
const _ = require('lodash');
// Define two objects to be compared
const obj1 = { a: 1, b: { c: 2, d: [3, 4] } };
const obj2 = { a: 1, b: { c: 2, d: [3, 4] } };
const customizer = (value1, value2) => {
// Check if both values are arrays
if (_.isArray(value1) && _.isArray(value2)) {
// If both values are arrays, compare them after sorting
return _.isEqual(_.sortBy(value1), _.sortBy(value2));
}
};
const isEqualWith = _.isEqualWith(obj1, obj2, customizer);
console.log(isEqualWith);
Output:
true
Using Lodash _.differenceWith() Method
In this approach, we use the _.differenceWith() method from Lodash to find the difference between two arrays of objects using a custom comparator function. This method returns an array of elements that are not present in the other array, helping to identify differences between the two objects.
Syntax:
_.differenceWith(array, [values], [comparator]);
Example: The below example shows how to compare two arrays of objects with Lodash using the _.differenceWith() method.
JavaScript
// Import the lodash library
const _ = require('lodash');
// Define two arrays of objects
const array1 = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
const array2 = [{ 'x': 1, 'y': 2 }, { 'x': 1, 'y': 2 }];
// Use differenceWith to find the difference
const difference = _.differenceWith(array1, array2, _.isEqual);
console.log(difference);
Output:
[ { x: 2, y: 1 } ]
Using Lodash _.isMatchWith() Method
In this approach, we use the _.isMatchWith() method to perform a partial comparison between two objects with custom comparison logic. This method allows you to customize how the comparison is done for specific properties.
Syntax:
_.isMatchWith(object, source, customizer);
Example: The below example shows how to compare two objects with Lodash using the _.isMatchWith() method.
JavaScript
// Import the lodash library
const _ = require('lodash');
// Define two objects to be compared
const obj1 = { a: 1, b: { c: 2, d: [3, 4] } };
const obj2 = { a: 1, b: { c: 2, d: [3, 5] } };
// Customizer function for comparison
const customizer = (objValue, srcValue) => {
if (_.isArray(objValue) && _.isArray(srcValue)) {
return _.difference(objValue, srcValue).length === 0;
}
};
// Use isMatchWith for partial comparison
const isMatchWith = _.isMatchWith(obj1, obj2, customizer);
console.log(isMatchWith);
Output:
false
Similar Reads
How to do a Deep Comparison Between Two Objects using Lodash?
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.: Table of Content Using _.isEqual() FunctionUsing _.isEqualWith()
4 min read
How to Filter Key of an Object using Lodash?
Filtering keys of an object involves selecting specific keys and creating a new object that contains only those keys. Using Lodash, this process allows you to include or exclude properties based on specific criteria, simplifying object manipulation. Below are the approaches to filter keys of an obje
2 min read
How to Replace Objects in Array using Lodash?
Replacing objects in an array using Lodash typically involves identifying the specific object(s) you want to replace and then performing the replacement operation. Lodash provides a range of methods to facilitate such operations, although the actual replacement might involve combining Lodash functio
3 min read
How to Convert Object Array to Hash Map using Lodash ?
Converting an Object Array to a Hash Map consists of manipulating the array elements to create a key-value mapping. Below are the different approaches to Converting an object array to a hash map using Lodash: Table of Content Using keyBy() functionUsing reduce functionRun the below command before ru
2 min read
How to Merge Array of Objects by Property using Lodash?
Merging an array of objects by property using Lodash is a common operation when you need to consolidate data that share a common identifier. For example, if you have a list of user objects and you want to combine them into a single object based on a unique property, Lodash provides useful functions
3 min read
How to Grouped Output using Lodash groupBy?
Lodash is a popular JavaScript utility library that simplifies common tasks like manipulating arrays, objects, strings, and more. we will explore how to add our own keys for grouped output using the lodash _.groupBy() function. This is very useful when we need to categorize data dynamically based on
3 min read
How to Compare Jagged Arrays using Lodash ?
Jagged arrays, or arrays of arrays, pose a unique challenge when it comes to comparison, especially when order doesn't matter. Below are the approaches to compare jagged arrays using Lodash: Table of Content Sorting and ComparingConverting to Sets and ComparingRun the below command to install Lodash
2 min read
How to Convert Object to Array in Lodash ?
Converting an Object to an Array consists of changing the data structure from key-value pairs to an array format. Below are the different approaches to converting objects to arrays in Lodash: Table of Content Using toArray function Using values functionRun the below command before running the below
2 min read
How to Compare Objects in JavaScript?
Comparing objects is not as simple as comparing numbers or strings. Objects are compared based on their memory references, so even if two objects have the same properties and values, they are considered distinct if they are stored in different memory locations. Below are the various approaches to co
3 min read
How to compare two JavaScript array objects using jQuery/JavaScript ?
In this article, we are given two JavaScript array/array objects and the task is to compare the equality of both array objects. These are the methods to compare two JavaScript array objects: Using jQuery not() methodUse the sort() functionUse JSON.stringify() functionUsing every() and indexOf()Using
3 min read