Function: isEqualWith()
ts
function isEqualWith<T>(
value,
other,
fn): boolean;Performs deep comparison with a custom comparator function. 使用自定义比较函数执行深度比较。
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
value | T | The first value to compare / 要比较的第一个值 |
other | T | The second value to compare / 要比较的第二个值 |
fn | (v1, v2) => boolean | undefined | Custom comparator function / 自定义比较函数 |
Returns
boolean
True if values are equivalent according to comparator / 如果根据比较器值相等则返回true
Example
typescript
// Compare arrays by length only
const byLength = (v1, v2) => Array.isArray(v1) && Array.isArray(v2) ? v1.length === v2.length : undefined
isEqualWith([1, 2, 3], [4, 5, 6], byLength) // => true
isEqualWith([1, 2], [4, 5, 6], byLength) // => false
// Case-insensitive string comparison
const caseInsensitive = (v1, v2) => typeof v1 === 'string' && typeof v2 === 'string'
? v1.toLowerCase() === v2.toLowerCase() : undefined
isEqualWith('Hello', 'HELLO', caseInsensitive) // => true
isEqualWith('Hello', 'World', caseInsensitive) // => false
// Falls back to isEqual if no custom function
isEqualWith([1, 2, 3], [1, 2, 3], null) // => trueSince
1.0.0