_.xorBy([arrays], [iteratee=_.identity])

59

_.xorBy([arrays], [iteratee=_.identity])

_xorBy与xor方法类似,求数组的对称差,区别是多传递一个iteratee方法,来处理每一个元素

参数

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

[iteratee=_.identity] (Function): 处理每一个元素的iteratee遍历器

返回值

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

例子

_.xorBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// => [1.2, 3.4]
 
// The `_.property` iteratee shorthand.
_.xorBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 2 }]

源代码:

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 `iteratee` which is
 * invoked for each element of each `arrays` to generate the criterion by
 * which they're compared. The order of result values is determined
 * by the order they occur in the arrays. The iteratee is invoked with one
 * argument: (value).
 *
 * @since 4.0.0
 * @category Array
 * @param {...Array} [arrays] The arrays to inspect.
 * @param {Function} iteratee The iteratee invoked per element.
 * @returns {Array} Returns the new array of filtered values.
 * @see difference, union, unionBy, unionWith, without, xor, xorWith
 * @example
 *
 * xorBy([2.1, 1.2], [2.3, 3.4], Math.floor)
 * // => [1.2, 3.4]
 */
//与xor方法类似,求数组的对称差,区别是多传递一个iteratee方法,来处理每一个元素
function xorBy(...arrays) {
  let iteratee = last(arrays)//最后一个参数,是iteratee
  if (isArrayLikeObject(iteratee)) {//如果iteratee不是函数,就赋值为undefined
    iteratee = undefined
  }
  return baseXor(filter(arrays, isArrayLikeObject), iteratee)
  //过滤掉arrays中的非array-like元素,然后传递给baseXor处理
}

export default xorBy
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

 

posted @ 2018-10-25 11:22  hahazexia  阅读(407)  评论(0)    收藏  举报