在 JavaScript 中获取两个数组的交集

在 JavaScript 中获取两个数组的交集

Get the Intersection of two Arrays in JavaScript

获取两个数组的交集:

  1. 将数组转换为Set对象以删除重复项。
  2. 将第一个转换Set回数组并调用filter()它的方法。
  3. 在每次迭代中,检查当前元素是否包含在第二个中
    Set
索引.js
function getIntersection(a, b) { const set1 = new Set(a); const set2 = new Set(b); const intersection = [...set1].filter( element => set2.has(element) ); return intersection; } const arr1 = ['a', 'b', 'c', 'c']; const arr2 = ['a', 'b', 'c', 'd', 'e', 'a']; // 👇️ ['a', 'b','c'] console.log(getIntersection(arr1, arr2));

我们使用
Set 对象
从两个数组中删除所有重复项。

Set对象仅存储唯一值,因此将数组转换为 aSet会自动删除所有重复值。
索引.js
const set1 = new Set(['a', 'a', 'a']); console.log(set1); // 👉️ Set(1) { 'a' }

我们这样做是因为我们不希望我们的交集包含重复值,例如['a', 'b', 'c', 'c'].

如果您希望交集包含重复值(如果有的话),请filter()直接使用该方法。

索引.js
const arr1 = ['a', 'b', 'c', 'c']; const arr2 = ['a', 'b', 'c', 'd', 'e', 'a']; const intersection = arr1.filter(element => arr2.includes(element)); console.log(intersection); // 👉️ [ 'a', 'b', 'c', 'c' ]

您还可以使用对该Array.filter()方法的两次调用来从交集数组中删除重复项。

索引.js
const arr1 = ['a', 'b', 'c', 'c']; const arr2 = ['a', 'b', 'c', 'd', 'e', 'a']; const intersection2 = arr1 .filter(element => arr2.includes(element)) .filter((element, index, arr3) => arr3.indexOf(element) === index); // 👇️ [ 'a', 'b', 'c' ] console.log(intersection2);

然后我们使用
扩展语法 (…)
将其转换
Set回数组并调用数组filter()上的方法。

索引.js
function getIntersection(a, b) { const set1 = new Set(a); const set2 = new Set(b); const intersection = [...set1].filter( element => set2.has(element) ); return intersection; } const arr1 = ['a', 'b', 'c', 'c']; const arr2 = ['a', 'b', 'c', 'd', 'e', 'a']; // 👇️ ['a', 'b','c'] console.log(getIntersection(arr1, arr2));

我们传递给
Array.filter()
方法的函数会随数组的每个元素一起调用。

在每次迭代中,我们检查当前元素是否包含在第二个中
Set

The filter() method returns an array containing the elements for which the
test function returns a truthy value.

In our case, these are all the elements that are contained in both arrays.

You can also use the Array.reduce() method to get the intersection of two or
more arrays.

Get the Intersection of two or more Arrays using reduce() #

To get the intersection of two or more arrays:

  1. Use the Array.reduce() method to iterate over the arrays.
  2. Use the filter() method to iterate over each array.
  3. Check if each element is contained in the other array.
index.js
const arr1 = ['a', 'b', 'c', 'c']; const arr2 = ['a', 'b', 'c', 'd', 'e', 'a']; const arr3 = ['a', 'b']; const arrays = [arr1, arr2, arr3]; const intersection = arrays.reduce((accumulator, array) => { return accumulator.filter(element => array.includes(element)); }); console.log(intersection);

We stored the arrays in a list and used the Array.reduce() method to iterate
over the two-dimensional list.

On each iteration, we use the Array.filter() method to iterate over the
accumulated array and check if all of the elements in the array are contained in
the next array.

The Array.reduce() method returns a new array that only contains the elements
that are contained in all of the supplied arrays.

You can also use the lodash library to get the intersection of two or more
arrays.

Get the Intersection of two or more Arrays using lodash #

To get the intersection of two or more arrays:

  1. Install the lodash library.
shell
npm install lodash # 👇️ only if you use TypeScript npm install @types/lodash --save-dev
  1. Use the intersection() method to get the intersection of two or more
    arrays.
index.js
import _ from 'lodash'; const arr1 = ['a', 'b', 'c', 'c']; const arr2 = ['a', 'b', 'c', 'd', 'e', 'a']; const intersection = _.intersection(arr1, arr2); console.log(intersection); // 👉️ [ 'a', 'b', 'c' ] const intersection3 = _.intersection(arr1, arr2, ['a', 'b']); console.log(intersection3); // 👉️ ['a', 'b']

The intersection() method from lodash creates an array of unique values that
are included in all of the provided arrays.

该方法可用于获取两个或多个数组的交集。

该方法返回一个新数组,其中包含所提供数组的共享值。