Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.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)
Parameters: This method accepts one parameter as mentioned above and described below:
- source: The object of property values to match.
Returns: [Function] It Returns the new spec function.
Example 1:
const _ = require( "lodash" );
var geek = [ { 'java' : 3, 'python' : 5, 'js' : 7},
{ 'java' : 4, 'python' : 2, 'js' : 6}
];
let gfg = _.filter(geek, _.matches({ 'java' : 3, 'js' : 7 }));
console.log(gfg)
|
Note: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.
Output:
[Object {java: 3, js: 7, python: 5}]
Example 2:
const _ = require( "lodash" );
var objects = [
{ 'a' : 1, 'b' : 2, 'c' : 3 },
{ 'a' : 4, 'b' : 5, 'c' : 6 },
{ 'a' : 8, 'b' : 7, 'c' : 9 }
];
gfg= _.filter(objects, _.matches({ 'a' : 8}));
console.log(gfg)
|
Note: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.
Output:
[Object {a: 8, b: 7, c: 9}]