docs/reference/compat/predicate/isEqualWith.md
::: warning Use es-toolkit's isEqualWith instead
This isEqualWith function operates slowly due to complex processing for Lodash compatibility.
Use the faster and more modern es-toolkit's isEqualWith instead.
:::
Checks if two values are equal using a custom comparison function.
const result = isEqualWith(a, b, customizer);
isEqualWith(a, b, areValuesEqual?)Deeply compares two values using a custom comparison function. If the custom function returns a boolean value, that result is used. If it returns undefined, the default equality comparison is used.
The custom comparison function is also used when comparing values inside complex structures like objects, arrays, Map, Set, etc., ensuring deep comparison.
import { isEqualWith } from 'es-toolkit/compat';
// Case-insensitive string comparison
const customizer = (a: any, b: any) => {
if (typeof a === 'string' && typeof b === 'string') {
return a.toLowerCase() === b.toLowerCase();
}
};
isEqualWith('Hello', 'hello', customizer); // true
isEqualWith({ a: 'Hello' }, { a: 'hello' }, customizer); // true
// Compare numbers by absolute value
const absCustomizer = (a: any, b: any) => {
if (typeof a === 'number' && typeof b === 'number') {
return Math.abs(a) === Math.abs(b);
}
};
isEqualWith([-1, 2], [1, -2], absCustomizer); // true
// Complex object comparison
const obj1 = {
name: 'JOHN',
details: { age: 30, city: 'NYC' },
};
const obj2 = {
name: 'john',
details: { age: 30, city: 'nyc' },
};
isEqualWith(obj1, obj2, customizer); // true
Special handling for Map and Set:
import { isEqualWith } from 'es-toolkit/compat';
const customizer = (a: any, b: any) => {
if (typeof a === 'string' && typeof b === 'string') {
return a.toLowerCase() === b.toLowerCase();
}
};
const map1 = new Map([['KEY', 'value']]);
const map2 = new Map([['key', 'value']]);
isEqualWith(map1, map2, customizer); // true
const set1 = new Set(['HELLO']);
const set2 = new Set(['hello']);
isEqualWith(set1, set2, customizer); // true
a (any): The first value to compare.b (any): The second value to compare.areValuesEqual ((x: any, y: any, property?: PropertyKey, xParent?: any, yParent?: any, stack?: Map<any, any>) => boolean | void): The custom comparison function.
x: Value from the first object ay: Value from the second object bproperty: The property key used to get x and yxParent: The parent object of the first value xyParent: The parent object of the second value ystack: Internal stack (Map) for handling circular references(boolean): Returns true if the values are equal according to the custom function, false otherwise.