zl程序教程

您现在的位置是:首页 >  其它

当前栏目

_.xorWith([arrays], [comparator])

Arrays _. Comparator
2023-09-11 14:15:02 时间

60

_.xorWith([arrays], [comparator])

_.xorWith与xor方法类似,求数组的对称差,区别是传递一个自定义比较方法comparator

参数

[arrays] (...Array): 需要求对称差的数组组成的数组

[comparator] (Function): 自定义比较方法

返回值

(Array): 返回过滤后的元素组成的新数组

例子

var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
 
_.xorWith(objects, others, _.isEqual);
// => [{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]

源代码:

import filter from './filter.js'
import baseXor from './.internal/baseXor.js'
import isArrayLikeObject from './isArrayLikeObject.js'
import last from './last.js'

/**
 * This method is like `xor` except that it accepts `comparator` which is
 * invoked to compare elements of `arrays`. The order of result values is
 * determined by the order they occur in the arrays. The comparator is invoked
 * with two arguments: (arrVal, othVal).
 *
 * @since 4.0.0
 * @category Array
 * @param {...Array} [arrays] The arrays to inspect.
 * @param {Function} [comparator] The comparator invoked per element.
 * @returns {Array} Returns the new array of filtered values.
 * @see difference, union, unionBy, unionWith, without, xor, xorBy
 * @example
 *
 * const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
 * const others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]
 *
 * xorWith(objects, others, isEqual)
 * // => [{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
 */
//与xor方法类似,求数组的对称差,区别是传递一个自定义比较方法comparator
function xorWith(...arrays) {
  let comparator = last(arrays)//最后一个参数是comparator自定义比较方法
  comparator = typeof comparator == 'function' ? comparator : undefined
  //如果comparator不是函数,就赋值为undefined
  return baseXor(filter(arrays, isArrayLikeObject), undefined, comparator)
  //去除arrays中的非array-like元素,然后调用baseXor处理
}

export default xorWith
baseXor
import baseDifference from './baseDifference.js'
import baseFlatten from './baseFlatten.js'
import baseUniq from './baseUniq.js'

/**
 * The base implementation of methods like `xor` which accepts an array of
 * arrays to inspect.
 *
 * @private
 * @param {Array} arrays The arrays to inspect.
 * @param {Function} [iteratee] The iteratee invoked per element.
 * @param {Function} [comparator] The comparator invoked per element.
 * @returns {Array} Returns the new array of values.
 */
//xor的基础实现
function baseXor(arrays, iteratee, comparator) {
  const length = arrays.length//有多少个需要求对称差的数组
  if (length < 2) {//如果只有一个数组,就传递给baseUniq做去重后直接返回结果
    return length ? baseUniq(arrays[0]) : []
  }
  let index = -1//数组参数的循环索引,也是结果数组的元素索引
  const result = new Array(length)//结果数组

  while (++index < length) {
    const array = arrays[index]//当前数组
    let othIndex = -1//每一个数组参数的元素的循环索引

    while (++othIndex < length) {
      if (othIndex != index) {
        //结果数组的元素等于当前array和下一个数组的差集元素
        //othIndex循环到下一个的时候,就不和当前array比较,和result[index比较]
        result[index] = baseDifference(result[index] || array, arrays[othIndex], iteratee, comparator)
      }
    }
  }
  return baseUniq(baseFlatten(result, 1), iteratee, comparator)
  //将结果数组里的数组都展开,然后去重
}

export default baseXor