Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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 Function for Filtering:
4.2.
JavaScript
4.3.
Finding an Object with a Specific Property Value:
4.4.
JavaScript
4.5.
Using with Complex Property Paths:
4.6.
JavaScript
4.7.
Filtering Based on Multiple Properties:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.matchesProperty() handle non-existent properties?
5.2.
Can _.matchesProperty() compare deep properties?
5.3.
Is _.matchesProperty() suitable for all data types?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.matchesProperty() Method

Author Riya Singh
0 upvote

Introduction

In JavaScript, particularly when dealing with arrays of objects or complex data structures, it's common to filter or find objects based on specific property values. Lodash's _.matchesProperty() method simplifies this task by creating a function that checks if an object's property value matches a given value. 

Lodash _.matchesProperty() Method

This method is particularly useful in scenarios such as filtering data, searching for objects with specific attributes, or validating object properties against certain criteria.

Why This Function is Used

The _.matchesProperty() function is used to create a predicate function for comparing a specific property of an object to a given value. This is essential in situations where you need to perform operations like filtering or searching in a collection based on the value of a particular property, allowing for more concise and readable code.

Syntax, Parameter and Return Value

Syntax:

 _.matchesProperty(path, srcValue)

Parameters:

  • path (Array|string): The path of the property to get.
     
  • srcValue: The value to match against.

Return Value: 

(Function) - Returns the new spec function.

Examples 

Creating a Predicate Function for Filtering:

  • JavaScript

JavaScript

var _ = require('lodash');

var users = [

 { 'user': 'barney', 'age': 36, 'active': true },

 { 'user': 'fred', 'age': 40, 'active': false }

];

var isActive = _.matchesProperty('active', true);

var activeUsers = _.filter(users, isActive);

console.log(activeUsers);
You can also try this code with Online Javascript Compiler
Run Code

Output:

 [{ 'user': 'barney', 'age': 36, 'active': true }]


Demonstrates using _.matchesProperty() to create a function for filtering based on the 'active' property.

Finding an Object with a Specific Property Value:

  • JavaScript

JavaScript

var products = [

 { 'name': 'apple', 'type': 'fruit', 'quantity': 30 },

 { 'name': 'carrot', 'type': 'vegetable', 'quantity': 50 }

];

var isFruit = _.matchesProperty('type', 'fruit');

var fruit = _.find(products, isFruit);

console.log(fruit);
You can also try this code with Online Javascript Compiler
Run Code

Output:

 { 'name': 'apple', 'type': 'fruit', 'quantity': 30 }


Shows how to find an object in an array with a specific property value.

Using with Complex Property Paths:

  • JavaScript

JavaScript

var data = [

 { 'item': { 'details': { 'id': 1, 'name': 'Item1' } } },

 { 'item': { 'details': { 'id': 2, 'name': 'Item2' } } }

];

var hasIdOne = _.matchesProperty('item.details.id', 1);

var itemWithIdOne = _.filter(data, hasIdOne);

console.log(itemWithIdOne);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

[{ 'item': { 'details': { 'id': 1, 'name': 'Item1' } } }]


An example of using _.matchesProperty() for matching objects with nested property values.

Filtering Based on Multiple Properties:

  • JavaScript

JavaScript

var books = [

 { 'title': 'Book1', 'author': 'Author1', 'year': 2000 },

 { 'title': 'Book2', 'author': 'Author2', 'year': 1995 }

];

var recentBook = _.matchesProperty('year', 2000);

var recentBooks = _.filter(books, recentBook);

console.log(recentBooks);
You can also try this code with Online Javascript Compiler
Run Code

Output:

 [{ 'title': 'Book1', 'author': 'Author1', 'year': 2000 }]


Demonstrates filtering an array of objects based on a specific property value.

Frequently Asked Questions

How does _.matchesProperty() handle non-existent properties?

If the specified property path does not exist in an object, the predicate function will return false.

Can _.matchesProperty() compare deep properties?

Yes, _.matchesProperty() can compare deep properties using dot or array syntax for the property path.

Is _.matchesProperty() suitable for all data types?

_.matchesProperty() works well with most data types. However, for complex data structures or custom comparison logic, additional handling might be required.

Conclusion

Lodash's _.matchesProperty() method is a versatile tool for creating predicate functions that check if a specific property of an object matches a given value. It's particularly useful for filtering, searching, and validating objects in collections based on their property values.

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