xorWith.js 1.1 KB

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