Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.isEqualWith() method of Lang in lodash is similar to _.isEqual() method and the only difference is that it accepts customizer which is called in order to compare values. Moreover, if the customizer used here returns undefined then the comparisons are dealt by the method instead.
Note: The customizer used here can be called with up to six arguments namely objValue, othValue, index|key, object, other, and stack.
Syntax:
_.isEqualWith(value, other, [customizer])
Parameters: This method accepts three parameters as mentioned above and described below:
- value: It is the value to be compared.
- other: It is the other value to be compared.
- customizer: It is the function which is used to customize comparisons.
Return Value: This method returns true if the stated values are equivalent otherwise it returns false.
Example 1:
Javascript
// Requiring lodash library const _ = require( 'lodash' ); // Defining a function portal function portal(val) { return /^G(?:fG|eeksforGeeks)$/.test(val); } // Defining customizer to compare values function customizer(objectValue, otherValue) { if (portal(objectValue) && portal(otherValue)) { return true ; } } // Initializing values var val = [ 'neveropen' , 'CS-portal' ]; var otherval = [ 'GfG' , 'CS-portal' ]; // Calling isEqualWith() method with all // its parameter let result = _.isEqualWith(val, otherval, customizer); // Displays output console.log(result); |
Output:
true
Example 2:
Javascript
// Requiring lodash library const _ = require( 'lodash' ); // Defining a function portal function portal(val) { return /^G(?:fG|eeksforGeeks)$/.test(val); } // Defining customizer to compare values function customizer(objectValue, otherValue) { if (portal(objectValue) && portal(otherValue)) { return true ; } } // Initializing values var val = [ 'neveropen' , 'CS-portal' ]; var otherval = [ 'GfG' , 'portal' ]; // Calling isEqualWith() method with all // its parameter let result = _.isEqualWith(val, otherval, customizer); // Displays output console.log(result); |
Output:
false
Example 3:
Javascript
// Requiring lodash library const _ = require( 'lodash' ); // Defining a function gfg function gfg(val) { return val; } // Defining customizer function intg(x, y) { if (gfg(x) === gfg(y)){ return true ; } } // Calling isEqualWith() method with all // its parameter let result = _.isEqualWith( 'gf' , 'gfg' , intg); // Displays output console.log(result); |
Output:
false
Reference: https://lodash.com/docs/4.17.15#isEqualWith