differenceBy.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var baseDifference = require('./_baseDifference'),
  2. baseFlatten = require('./_baseFlatten'),
  3. baseIteratee = require('./_baseIteratee'),
  4. isArrayLikeObject = require('./isArrayLikeObject'),
  5. last = require('./last'),
  6. rest = require('./rest');
  7. /**
  8. * This method is like `_.difference` except that it accepts `iteratee` which
  9. * is invoked for each element of `array` and `values` to generate the criterion
  10. * by which they're compared. Result values are chosen from the first array.
  11. * The iteratee is invoked with one argument: (value).
  12. *
  13. * @static
  14. * @memberOf _
  15. * @since 4.0.0
  16. * @category Array
  17. * @param {Array} array The array to inspect.
  18. * @param {...Array} [values] The values to exclude.
  19. * @param {Array|Function|Object|string} [iteratee=_.identity]
  20. * The iteratee invoked per element.
  21. * @returns {Array} Returns the new array of filtered values.
  22. * @example
  23. *
  24. * _.differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);
  25. * // => [1.2]
  26. *
  27. * // The `_.property` iteratee shorthand.
  28. * _.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');
  29. * // => [{ 'x': 2 }]
  30. */
  31. var differenceBy = rest(function(array, values) {
  32. var iteratee = last(values);
  33. if (isArrayLikeObject(iteratee)) {
  34. iteratee = undefined;
  35. }
  36. return isArrayLikeObject(array)
  37. ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true), baseIteratee(iteratee))
  38. : [];
  39. });
  40. module.exports = differenceBy;