Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Creating a Predicate for Filtering:
4.2.
JavaScript
4.3.
Matching Objects in an Array:
4.4.
JavaScript
4.5.
Using with Complex Data Structures:
4.6.
JavaScript
4.7.
Implementing Custom Search Functionality:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.matches() differ from simple property checking?
5.2.
Can _.matches() handle arrays and other non-object values?
5.3.
Is _.matches() efficient for large data sets?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.matches() Method

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In JavaScript programming, especially when working with collections or complex data structures, it's often necessary to check if objects in a collection match a certain criteria. Lodash's _.matches() method provides a concise and efficient way to create a function that performs such checks. 

Lodash _.matches() Method

This method is particularly useful for creating predicates for filtering operations, validating data structures, or implementing custom search and match functionalities.

Why This Function is Used

The _.matches() function is used to create a function that performs a deep comparison between the properties of an object and a given source object. It's essential in scenarios where you need to filter or find objects in a collection that conform to a specific pattern or structure, ensuring a more streamlined and readable approach to data manipulation.

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

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass