UNPKG

1.26 kBJavaScriptView Raw
1import baseDifference from './_baseDifference';
2import baseFlatten from './_baseFlatten';
3import isArrayLikeObject from './isArrayLikeObject';
4import last from './last';
5import rest from './rest';
6
7/**
8 * This method is like `_.difference` except that it accepts `comparator`
9 * which is invoked to compare elements of `array` to `values`. Result values
10 * are chosen from the first array. The comparator is invoked with two arguments:
11 * (arrVal, othVal).
12 *
13 * @static
14 * @memberOf _
15 * @since 4.0.0
16 * @category Array
17 * @param {Array} array The array to inspect.
18 * @param {...Array} [values] The values to exclude.
19 * @param {Function} [comparator] The comparator invoked per element.
20 * @returns {Array} Returns the new array of filtered values.
21 * @example
22 *
23 * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
24 *
25 * _.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
26 * // => [{ 'x': 2, 'y': 1 }]
27 */
28var differenceWith = rest(function(array, values) {
29 var comparator = last(values);
30 if (isArrayLikeObject(comparator)) {
31 comparator = undefined;
32 }
33 return isArrayLikeObject(array)
34 ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true), undefined, comparator)
35 : [];
36});
37
38export default differenceWith;