_.isEqualWith(value, other, [customizer])

137ui

_.isEqualWith(value, other, [customizer])

_.isEqualWith此方法相似isEqual區別是它接受一個自定義比較方法。若是自定義比較方法返回值是undefined,比較結果就由baseIsEqual來處理spa

參數code

value (*): 須要比較的值
other (*): 須要比較的另一個值
[customizer] (Function): 自定義比較方法blog

返回值it

(boolean): 若是兩個值相等返回true,不然falseio

例子function

function isGreeting(value) { return /^h(?:i|ello)$/.test(value); } function customizer(objValue, othValue) { if (isGreeting(objValue) && isGreeting(othValue)) { return true; } } var array = ['hello', 'goodbye']; var other = ['hi', 'goodbye']; _.isEqualWith(array, other, customizer); // => true

源代碼class

import baseIsEqual from './.internal/baseIsEqual.js'

/** * This method is like `isEqual` except that it accepts `customizer` which * is invoked to compare values. If `customizer` returns `undefined`, comparisons * are handled by the method instead. The `customizer` is invoked with up to * six arguments: (objValue, othValue [, index|key, object, other, stack]). * * @since 4.0.0 * @category Lang * @param {*} value The value to compare. * @param {*} other The other value to compare. * @param {Function} [customizer] The function to customize comparisons. * @returns {boolean} Returns `true` if the values are equivalent, else `false`. * @example * * function isGreeting(value) { * return /^h(?:i|ello)$/.test(value) * } * * function customizer(objValue, othValue) { * if (isGreeting(objValue) && isGreeting(othValue)) { * return true * } * } * * const array = ['hello', 'goodbye'] * const other = ['hi', 'goodbye'] * * isEqualWith(array, other, customizer) * // => true */
//此方法相似isEqual區別是它接受一個自定義比較方法。若是自定義比較方法返回值是undefined,比較結果就由baseIsEqual來處理
function isEqualWith(value, other, customizer) { customizer = typeof customizer == 'function' ? customizer : undefined//判斷是否傳遞了customizer
  const result = customizer ? customizer(value, other) : undefined//由customizer計算比較結果
  return result === undefined ? baseIsEqual(value, other, undefined, customizer) : !!result//若是customizer的結果是undefined,調用baseIsEqual處理
} export default isEqualWith
相關文章
相關標籤/搜索