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
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
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
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
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 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.