Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var baseIndexOf = require('../internal/baseIndexOf'),
2
cacheIndexOf = require('../internal/cacheIndexOf'),
3
createCache = require('../internal/createCache'),
4
isArrayLike = require('../internal/isArrayLike'),
5
restParam = require('../function/restParam');
6
7
/**
8
* Creates an array of unique values that are included in all of the provided
9
* arrays using [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
10
* for equality comparisons.
11
*
12
* @static
13
* @memberOf _
14
* @category Array
15
* @param {...Array} [arrays] The arrays to inspect.
16
* @returns {Array} Returns the new array of shared values.
17
* @example
18
* _.intersection([1, 2], [4, 2], [2, 1]);
19
* // => [2]
20
*/
21
var intersection = restParam(function(arrays) {
22
var othLength = arrays.length,
23
othIndex = othLength,
24
caches = Array(length),
25
indexOf = baseIndexOf,
26
isCommon = true,
27
result = [];
28
29
while (othIndex--) {
30
var value = arrays[othIndex] = isArrayLike(value = arrays[othIndex]) ? value : [];
31
caches[othIndex] = (isCommon && value.length >= 120) ? createCache(othIndex && value) : null;
32
}
33
var array = arrays[0],
34
index = -1,
35
length = array ? array.length : 0,
36
seen = caches[0];
37
38
outer:
39
while (++index < length) {
40
value = array[index];
41
if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value, 0)) < 0) {
42
var othIndex = othLength;
43
while (--othIndex) {
44
var cache = caches[othIndex];
45
if ((cache ? cacheIndexOf(cache, value) : indexOf(arrays[othIndex], value, 0)) < 0) {
46
continue outer;
47
}
48
}
49
if (seen) {
50
seen.push(value);
51
}
52
result.push(value);
53
}
54
}
55
return result;
56
});
57
58
module.exports = intersection;
59
60