differenceBy.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var baseDifference = require('./_baseDifference'),
  2. baseFlatten = require('./_baseFlatten'),
  3. baseIteratee = require('./_baseIteratee'),
  4. baseRest = require('./_baseRest'),
  5. isArrayLikeObject = require('./isArrayLikeObject'),
  6. last = require('./last');
  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. The order and references of result values are
  11. * determined by the first array. The iteratee is invoked with one argument:
  12. * (value).
  13. *
  14. * **Note:** Unlike `_.pullAllBy`, this method returns a new array.
  15. *
  16. * @static
  17. * @memberOf _
  18. * @since 4.0.0
  19. * @category Array
  20. * @param {Array} array The array to inspect.
  21. * @param {...Array} [values] The values to exclude.
  22. * @param {Function} [iteratee=_.identity] The iteratee invoked per element.
  23. * @returns {Array} Returns the new array of filtered values.
  24. * @example
  25. *
  26. * _.differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);
  27. * // => [1.2]
  28. *
  29. * // The `_.property` iteratee shorthand.
  30. * _.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');
  31. * // => [{ 'x': 2 }]
  32. */
  33. var differenceBy = baseRest(function(array, values) {
  34. var iteratee = last(values);
  35. if (isArrayLikeObject(iteratee)) {
  36. iteratee = undefined;
  37. }
  38. return isArrayLikeObject(array)
  39. ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true), baseIteratee(iteratee, 2))
  40. : [];
  41. });
  42. module.exports = differenceBy;