How to use Lodash _.isMatch() Method In Lodash
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.
// 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
How to Compare Two Objects using Lodash?
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:
Table of Content
- Using Lodash _.isEqual() Method
- Using Lodash _.isMatch() Method
- Using _.isEqualWith() Method
Run the below command to install Lodash JavaScript Library:
npm install lodash