Lodash _.matches() Method
Lodash _.matches method creates a function that performs a partial deep comparison between a given object and source, returning true if the given object has equivalent property values, else false.
Syntax:
_.matches(source);
Parameter:
- source: The object of property values to match.
Return Value:
It returns the new spec function.
Example 1: In this example, we are printing the matching value to the given array of objects by using the _.matches() method.
javascript
// Requiring the lodash library const _ = require( "lodash" ); // Using _.matches() method let geek = [{ 'java' : 3, 'python' : 5, 'js' : 7 }, { 'java' : 4, 'python' : 2, 'js' : 6 } ]; let gfg = _.filter(geek, _.matches({ 'java' : 3, 'js' : 7 })); // Storing the Result console.log(gfg); |
Output:
[Object {java: 3, js: 7, python: 5}]
Example 2: In this example, we are printing the matching value to the given array of objects by using the _.matches() method.
javascript
// Requiring the lodash library const _ = require( "lodash" ); // Using _.matches() method let objects = [ { 'a' : 1, 'b' : 2, 'c' : 3 }, { 'a' : 4, 'b' : 5, 'c' : 6 }, { 'a' : 8, 'b' : 7, 'c' : 9 } ]; let gfg = _.filter(objects, _.matches({ 'a' : 8 })); console.log(gfg) |
Output:
[ {a: 8, b: 7, c: 9}]