xorBy.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. var arrayFilter = require('./_arrayFilter'),
  2. baseIteratee = require('./_baseIteratee'),
  3. baseRest = require('./_baseRest'),
  4. baseXor = require('./_baseXor'),
  5. isArrayLikeObject = require('./isArrayLikeObject'),
  6. last = require('./last');
  7. /**
  8. * This method is like `_.xor` except that it accepts `iteratee` which is
  9. * invoked for each element of each `arrays` to generate the criterion by
  10. * which by which they're compared. The order of result values is determined
  11. * by the order they occur in the arrays. The iteratee is invoked with one
  12. * argument: (value).
  13. *
  14. * @static
  15. * @memberOf _
  16. * @since 4.0.0
  17. * @category Array
  18. * @param {...Array} [arrays] The arrays to inspect.
  19. * @param {Function} [iteratee=_.identity] The iteratee invoked per element.
  20. * @returns {Array} Returns the new array of filtered values.
  21. * @example
  22. *
  23. * _.xorBy([2.1, 1.2], [2.3, 3.4], Math.floor);
  24. * // => [1.2, 3.4]
  25. *
  26. * // The `_.property` iteratee shorthand.
  27. * _.xorBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
  28. * // => [{ 'x': 2 }]
  29. */
  30. var xorBy = baseRest(function(arrays) {
  31. var iteratee = last(arrays);
  32. if (isArrayLikeObject(iteratee)) {
  33. iteratee = undefined;
  34. }
  35. return baseXor(arrayFilter(arrays, isArrayLikeObject), baseIteratee(iteratee, 2));
  36. });
  37. module.exports = xorBy;