xorWith.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. var arrayFilter = require('./_arrayFilter'),
  2. baseRest = require('./_baseRest'),
  3. baseXor = require('./_baseXor'),
  4. isArrayLikeObject = require('./isArrayLikeObject'),
  5. last = require('./last');
  6. /**
  7. * This method is like `_.xor` except that it accepts `comparator` which is
  8. * invoked to compare elements of `arrays`. The order of result values is
  9. * determined by the order they occur in the arrays. The comparator is invoked
  10. * with two arguments: (arrVal, othVal).
  11. *
  12. * @static
  13. * @memberOf _
  14. * @since 4.0.0
  15. * @category Array
  16. * @param {...Array} [arrays] The arrays to inspect.
  17. * @param {Function} [comparator] The comparator invoked per element.
  18. * @returns {Array} Returns the new array of filtered values.
  19. * @example
  20. *
  21. * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
  22. * var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
  23. *
  24. * _.xorWith(objects, others, _.isEqual);
  25. * // => [{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
  26. */
  27. var xorWith = baseRest(function(arrays) {
  28. var comparator = last(arrays);
  29. comparator = typeof comparator == 'function' ? comparator : undefined;
  30. return baseXor(arrayFilter(arrays, isArrayLikeObject), undefined, comparator);
  31. });
  32. module.exports = xorWith;