UNPKG

1.15 kBJavaScriptView Raw
1import baseFlatten from './_baseFlatten';
2import baseUniq from './_baseUniq';
3import isArrayLikeObject from './isArrayLikeObject';
4import last from './last';
5import rest from './rest';
6
7/**
8 * This method is like `_.union` except that it accepts `comparator` which
9 * is invoked to compare elements of `arrays`. 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 */
27var unionWith = rest(function(arrays) {
28 var comparator = last(arrays);
29 if (isArrayLikeObject(comparator)) {
30 comparator = undefined;
31 }
32 return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true), undefined, comparator);
33});
34
35export default unionWith;