You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unionBy.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. var baseFlatten = require('./_baseFlatten'),
  2. baseIteratee = require('./_baseIteratee'),
  3. baseRest = require('./_baseRest'),
  4. baseUniq = require('./_baseUniq'),
  5. isArrayLikeObject = require('./isArrayLikeObject'),
  6. last = require('./last');
  7. /**
  8. * This method is like `_.union` except that it accepts `iteratee` which is
  9. * invoked for each element of each `arrays` to generate the criterion by
  10. * which uniqueness is computed. Result values are chosen from the first
  11. * array in which the value occurs. The iteratee is invoked with one argument:
  12. * (value).
  13. *
  14. * @static
  15. * @memberOf _
  16. * @since 4.0.0
  17. * @category Array
  18. * @param {...Array} [arrays] The arrays to inspect.
  19. * @param {Function} [iteratee=_.identity] The iteratee invoked per element.
  20. * @returns {Array} Returns the new array of combined values.
  21. * @example
  22. *
  23. * _.unionBy([2.1], [1.2, 2.3], Math.floor);
  24. * // => [2.1, 1.2]
  25. *
  26. * // The `_.property` iteratee shorthand.
  27. * _.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
  28. * // => [{ 'x': 1 }, { 'x': 2 }]
  29. */
  30. var unionBy = baseRest(function(arrays) {
  31. var iteratee = last(arrays);
  32. if (isArrayLikeObject(iteratee)) {
  33. iteratee = undefined;
  34. }
  35. return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true), baseIteratee(iteratee, 2));
  36. });
  37. module.exports = unionBy;