Skip to content

Function: isEqualWith()

ts
function isEqualWith<T>(
   value, 
   other, 
   fn): boolean;

Performs deep comparison with a custom comparator function. 使用自定义比较函数执行深度比较。

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
valueTThe first value to compare / 要比较的第一个值
otherTThe second value to compare / 要比较的第二个值
fn(v1, v2) => boolean | undefinedCustom 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) // => true

Since

1.0.0

Released under the MIT License.