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.

34 lines
1.2 KiB

пре 4 година
  1. var baseFlatten = require('./_baseFlatten'),
  2. baseRest = require('./_baseRest'),
  3. baseUniq = require('./_baseUniq'),
  4. isArrayLikeObject = require('./isArrayLikeObject'),
  5. last = require('./last');
  6. /**
  7. * This method is like `_.union` except that it accepts `comparator` which
  8. * is invoked to compare elements of `arrays`. Result values are chosen from
  9. * the first array in which the value occurs. The comparator is invoked
  10. * with two arguments: (arrVal, othVal).
  11. *
  12. * @static
  13. * @memberOf _
  14. * @since 4.0.0
  15. * @category Array
  16. * @param {...Array} [arrays] The arrays to inspect.
  17. * @param {Function} [comparator] The comparator invoked per element.
  18. * @returns {Array} Returns the new array of combined values.
  19. * @example
  20. *
  21. * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
  22. * var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
  23. *
  24. * _.unionWith(objects, others, _.isEqual);
  25. * // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
  26. */
  27. var unionWith = baseRest(function(arrays) {
  28. var comparator = last(arrays);
  29. comparator = typeof comparator == 'function' ? comparator : undefined;
  30. return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true), undefined, comparator);
  31. });
  32. module.exports = unionWith;