How to use the matchesProperty function from lodash

Find comprehensive JavaScript lodash.matchesProperty code examples handpicked from public code repositorys.

lodash.matchesProperty creates a function that matches a property value of a given object.

252
253
254
255
256
257
258
259
260
261
module.exports.mapArgsWith         = _.mapArgsWith;
module.exports.mapKeys             = _.mapKeys;
module.exports.mapValues           = _.mapValues;
module.exports.mapcat              = _.mapcat;
module.exports.matches             = _.matches;
module.exports.matchesProperty     = _.matchesProperty;
module.exports.max                 = _.max;
module.exports.maxBy               = _.maxBy;
module.exports.mean                = _.mean;
module.exports.meanBy              = _.meanBy;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

911
912
913
914
915
916
917
918
919
920
921
922
923
console.log(iteratee); // => [Function: matches]


const matches = _.matches({ 'a': 1, 'b': 2 });
console.log(matches({ 'a': 1, 'b': 2, 'c': 3 })); // => true


const matchesProperty = _.matchesProperty('a', 1);
console.log(matchesProperty({ 'a': 1, 'b': 2 })); // => true


const method = _.method('a.b');
console.log(method({ 'a': { 'b': _.constant(2) } })); // => 2
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.matchesProperty work?

lodash.matchesProperty creates a function that compares the value of a given property of an object to a given value and returns true if they match.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const users = [
  { id: 1, name: "Alice", isActive: true },
  { id: 2, name: "Bob", isActive: false },
  { id: 3, name: "Charlie", isActive: true },
];

const isActiveUser = _.matchesProperty("isActive", true);

const activeUsers = users.filter(isActiveUser);

console.log(activeUsers); // [{ id: 1, name: 'Alice', isActive: true }, { id: 3, name: 'Charlie', isActive: true }]

In the example above, lodash.matchesProperty is used to create a predicate function called isActiveUser that matches an object based on the isActive key having a value of true. This function is then used with the Array.filter() method to filter out only the active users from the users array.

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)