Syntax, Parameter and Return Value
Syntax:
_.matches(source)
Parameters:
source (Object): The object to match against.
Return Value:
(Function) - Returns a new function that checks if a given object matches the source object's property values.
Examples
Creating a Predicate for Filtering:
JavaScript
var _ = require('lodash');
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }
];
var matchesActiveUser = _.matches({ 'active': true });
var activeUsers = _.filter(users, matchesActiveUser);
console.log(activeUsers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'user': 'barney', 'age': 36, 'active': true }]
Demonstrates using _.matches() to create a function for filtering active users.
Matching Objects in an Array:
JavaScript
var products = [
{ 'name': 'apple', 'category': 'fruit', 'stock': 10 },
{ 'name': 'carrot', 'category': 'vegetable', 'stock': 5 }
];
var fruitMatcher = _.matches({ 'category': 'fruit' });
var fruits = _.find(products, fruitMatcher);
console.log(fruits);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'name': 'apple', 'category': 'fruit', 'stock': 10 }
Shows creating a matcher function to find objects in an array based on category.
Using with Complex Data Structures:
JavaScript
var data = [
{ 'id': 1, 'details': { 'type': 'admin', 'active': true } },
{ 'id': 2, 'details': { 'type': 'user', 'active': false } }
];
var adminMatcher = _.matches({ 'details': { 'type': 'admin' } });
var admins = _.filter(data, adminMatcher);
console.log(admins);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'id': 1, 'details': { 'type': 'admin', 'active': true } }]
An example of using _.matches() for matching objects with nested properties.
Implementing Custom Search Functionality:
JavaScript
var items = [
{ 'name': 'Chair', 'color': 'blue', 'price': 45 },
{ 'name': 'Desk', 'color': 'white', 'price': 120 }
];
var whiteItemsMatcher = _.matches({ 'color': 'white' });
var whiteItems = _.filter(items, whiteItemsMatcher);
console.log(whiteItems);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'name': 'Desk', 'color': 'white', 'price': 120 }]
Demonstrates creating a matcher function for custom search functionality based on color.
Frequently Asked Questions
How does _.matches() differ from simple property checking?
_.matches() performs a deep comparison, allowing you to match against nested properties and more complex structures, whereas simple property checking typically involves direct value comparisons.
Can _.matches() handle arrays and other non-object values?
_.matches() is designed to work with object structures. For arrays or other types of data, different methods or custom logic would be more appropriate.
Is _.matches() efficient for large data sets?
While _.matches() is generally efficient, the performance may vary depending on the complexity of the data structure and the size of the data set. For very large or complex data, performance considerations may require more optimized solutions.
Conclusion
Lodash's _.matches() method is a versatile tool for creating functions that perform deep comparisons of objects against a source pattern. It's particularly beneficial in filtering, searching, and validating operations involving collections and complex data structures.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.