uniqBy.js 957 B

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 iteratee is invoked with one argument: (value).
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 4.0.0
  11. * @category Array
  12. * @param {Array} array The array to inspect.
  13. * @param {Array|Function|Object|string} [iteratee=_.identity]
  14. * The iteratee invoked per element.
  15. * @returns {Array} Returns the new duplicate free array.
  16. * @example
  17. *
  18. * _.uniqBy([2.1, 1.2, 2.3], Math.floor);
  19. * // => [2.1, 1.2]
  20. *
  21. * // The `_.property` iteratee shorthand.
  22. * _.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
  23. * // => [{ 'x': 1 }, { 'x': 2 }]
  24. */
  25. function uniqBy(array, iteratee) {
  26. return (array && array.length)
  27. ? baseUniq(array, baseIteratee(iteratee))
  28. : [];
  29. }
  30. module.exports = uniqBy;