uniqWith.js 958 B

1234567891011121314151617181920212223242526272829
  1. var baseUniq = require('./_baseUniq');
  2. /**
  3. * This method is like `_.uniq` except that it accepts `comparator` which
  4. * is invoked to compare elements of `array`. The order of result values is
  5. * determined by the order they occur in the array.The comparator is invoked
  6. * with two arguments: (arrVal, othVal).
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 4.0.0
  11. * @category Array
  12. * @param {Array} array The array to inspect.
  13. * @param {Function} [comparator] The comparator invoked per element.
  14. * @returns {Array} Returns the new duplicate free array.
  15. * @example
  16. *
  17. * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
  18. *
  19. * _.uniqWith(objects, _.isEqual);
  20. * // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
  21. */
  22. function uniqWith(array, comparator) {
  23. comparator = typeof comparator == 'function' ? comparator : undefined;
  24. return (array && array.length) ? baseUniq(array, undefined, comparator) : [];
  25. }
  26. module.exports = uniqWith;