differenceWith.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var baseDifference = require('./_baseDifference'),
  2. baseFlatten = require('./_baseFlatten'),
  3. baseRest = require('./_baseRest'),
  4. isArrayLikeObject = require('./isArrayLikeObject'),
  5. last = require('./last');
  6. /**
  7. * This method is like `_.difference` except that it accepts `comparator`
  8. * which is invoked to compare elements of `array` to `values`. The order and
  9. * references of result values are determined by the first array. The comparator
  10. * is invoked with two arguments: (arrVal, othVal).
  11. *
  12. * **Note:** Unlike `_.pullAllWith`, this method returns a new array.
  13. *
  14. * @static
  15. * @memberOf _
  16. * @since 4.0.0
  17. * @category Array
  18. * @param {Array} array The array to inspect.
  19. * @param {...Array} [values] The values to exclude.
  20. * @param {Function} [comparator] The comparator invoked per element.
  21. * @returns {Array} Returns the new array of filtered values.
  22. * @example
  23. *
  24. * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
  25. *
  26. * _.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
  27. * // => [{ 'x': 2, 'y': 1 }]
  28. */
  29. var differenceWith = baseRest(function(array, values) {
  30. var comparator = last(values);
  31. if (isArrayLikeObject(comparator)) {
  32. comparator = undefined;
  33. }
  34. return isArrayLikeObject(array)
  35. ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true), undefined, comparator)
  36. : [];
  37. });
  38. module.exports = differenceWith;