uniqBy.js 1013B

1234567891011121314151617181920212223242526272829303132
  1. var baseIteratee = require('./_baseIteratee'),
  2. baseUniq = require('./_baseUniq');
  3. /**
  4. * This method is like `_.uniq` except that it accepts `iteratee` which is
  5. * invoked for each element in `array` to generate the criterion by which
  6. * uniqueness is computed. The order of result values is determined by the
  7. * order they occur in the array. The iteratee is invoked with one argument:
  8. * (value).
  9. *
  10. * @static
  11. * @memberOf _
  12. * @since 4.0.0
  13. * @category Array
  14. * @param {Array} array The array to inspect.
  15. * @param {Function} [iteratee=_.identity] The iteratee invoked per element.
  16. * @returns {Array} Returns the new duplicate free array.
  17. * @example
  18. *
  19. * _.uniqBy([2.1, 1.2, 2.3], Math.floor);
  20. * // => [2.1, 1.2]
  21. *
  22. * // The `_.property` iteratee shorthand.
  23. * _.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
  24. * // => [{ 'x': 1 }, { 'x': 2 }]
  25. */
  26. function uniqBy(array, iteratee) {
  27. return (array && array.length) ? baseUniq(array, baseIteratee(iteratee, 2)) : [];
  28. }
  29. module.exports = uniqBy;