1 | import arrayMap from './_arrayMap';
|
2 | import baseCastArrayLikeObject from './_baseCastArrayLikeObject';
|
3 | import baseIntersection from './_baseIntersection';
|
4 | import baseIteratee from './_baseIteratee';
|
5 | import last from './last';
|
6 | import rest from './rest';
|
7 |
|
8 | /**
|
9 | * This method is like `_.intersection` except that it accepts `iteratee`
|
10 | * which is invoked for each element of each `arrays` to generate the criterion
|
11 | * by which they're compared. Result values are chosen from the first array.
|
12 | * The iteratee is invoked with one argument: (value).
|
13 | *
|
14 | * @static
|
15 | * @memberOf _
|
16 | * @since 4.0.0
|
17 | * @category Array
|
18 | * @param {...Array} [arrays] The arrays to inspect.
|
19 | * @param {Array|Function|Object|string} [iteratee=_.identity]
|
20 | * The iteratee invoked per element.
|
21 | * @returns {Array} Returns the new array of intersecting values.
|
22 | * @example
|
23 | *
|
24 | * _.intersectionBy([2.1, 1.2], [4.3, 2.4], Math.floor);
|
25 | * // => [2.1]
|
26 | *
|
27 | * // The `_.property` iteratee shorthand.
|
28 | * _.intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
|
29 | * // => [{ 'x': 1 }]
|
30 | */
|
31 | var intersectionBy = rest(function(arrays) {
|
32 | var iteratee = last(arrays),
|
33 | mapped = arrayMap(arrays, baseCastArrayLikeObject);
|
34 |
|
35 | if (iteratee === last(mapped)) {
|
36 | iteratee = undefined;
|
37 | } else {
|
38 | mapped.pop();
|
39 | }
|
40 | return (mapped.length && mapped[0] === arrays[0])
|
41 | ? baseIntersection(mapped, baseIteratee(iteratee))
|
42 | : [];
|
43 | });
|
44 |
|
45 | export default intersectionBy;
|