首页  >  文章  >  web前端  >  如何计算 JavaScript 数组的并集?

如何计算 JavaScript 数组的并集?

王林
王林转载
2023-09-10 18:49:091136浏览

如何计算 JavaScript 数组的并集?

我们可以通过合并两个数组并删除重复元素来获得两个数组的并集,并且并集给出两个数组中唯一的元素。

在本教程中,我们将学习使用各种方法计算 JavaScript 数组的并集。

使用 set() 数据结构

第一种方法是使用 set() 数据结构。集合数据结构只能包含唯一元素。我们可以将两个数组的所有元素添加到集合中,并从集合的元素创建一个新数组以创建并集。

语法

用户可以按照下面的语法使用集合数据结构来计算 JavaScript 数组的并集。

let union = [...new Set([...array1, ...array2])];

在上面的语法中,我们使用扩展运算符连接两个数组并从结果数组创建一个新集合。之后,我们再次使用展开运算符将集合的元素添加到数组中。

示例 1

在下面的示例中,array1 和 array2 包含一些公共数字。首先,我们使用扩展运算符联系 array1 和 array2。之后,我们使用 new Set() 构造函数从结果数组创建一个集合。该集合只能包含唯一的元素。因此,它包含两个数组并集中的所有元素。

之后,我们使用扩展运算符将集合的所有元素添加到联合数组中。在输出中,用户可以观察两个数组的并集。

<html>
<body>
   <h3> Using the <i> set() </i> data structure to compute the union of two arrays in JavaScript </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      
      // using the set() to compute union
      let array1 = [1, 2, 3, 4, 5];
      let array2 = [4, 5, 6, 7, 8];
      let union = [...new Set([...array1, ...array2])];
      output.innerHTML = "The first array is " + array1 + "<br>";
      output.innerHTML += "The second array is " + array2 + "<br>";
      output.innerHTML += "The union of the two arrays is " + union + "<br>";
   </script>
</body>
</html>

使用对象计算 JavaScript 数组的并集

在这种方法中,我们可以添加数组值作为对象属性。该对象始终包含唯一的键。因此,我们可以使用数组元素作为对象的键以及该特定键的任何值。之后,我们可以获取所有对象键来获取数组的并集。

语法

用户可以按照下面的语法使用对象来计算数组的并集。

for () {
   union[names1[i]] = 1;
}
for (let key in the union) {
   finalArray.push(key);
}

在上面的语法中,首先,我们将所有数组元素作为对象的键推送。之后,我们从对象中获取键并将它们添加到数组中。

示例 2

在下面的示例中,我们有两个名为names1和names2的数组。我们添加第一个数组的元素作为对象的键。之后,我们添加第二个数组的元素作为对象的键。

接下来,我们遍历对象,获取对象的键并将其推送到 FinalArray。 FinalArray 包含名称 1 和名称 2 数组的并集。

<html>
<body>
   <h3> Using the <i> object </i> to compute the union of two arrays in JavaScript </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      
      // using the object to compute the union of two arrays
      let names1 = ["John", "Peter", "Sally", "Jane", "Mark"];
      let names2 = ["Peter", "Sally", "Greg"];
      let union = {};
      
      //Add all the elements of the first array to the object
      for (let i = 0; i < names1.length; i++) {
         union[names1[i]] = 1;
      }
      for (let i = 0; i < names2.length; i++) {
         union[names2[i]] = 1;
      }
      
      //Convert the object to an array
      let finalArray = [];
      for (let key in union) {
         finalArray.push(key);
      }
      output.innerHTML = "First array: " + JSON.stringify(names1) + "<br>";
      output.innerHTML += "Second array: " + JSON.stringify(names2) + "<br>";
      output.innerHTML += "The union of the two arrays: " + JSON.stringify(finalArray) + "<br>";
   </script>
</body>
</html>

使用 Filter() 和 Concat() 方法

concat() 方法用于合并两个或多个数组。我们可以使用 filter() 方法合并两个数组后过滤唯一元素。这样,我们就可以使用 concat() 和 filter() 方法来计算数组的并集。

语法

用户可以按照下面的语法使用filter()和concat()方法来计算数组的并集。

let cities = cities1.concat(cities2);
cities.sort();
let unionCities = cities.filter((value, index) => cities.indexOf(value) === index);

在上面的语法中,我们首先合并两个数组,对它们进行排序,然后使用filter()方法过滤唯一元素。

示例 3

在下面的示例中,city1 和 citites2 数组包含一些城市名称,其中一些是常见的。城市数组包含两个数组的数组元素。

之后,我们使用 sort() 方法对城市数组进行排序。接下来,我们使用 filter() 方法从城市数组中过滤唯一值。在filter()方法中,我们将回调函数作为参数传递,该函数检查当前城市的索引是否等于当前索引以删除重复元素。

<html>
<body>
   <h3> Using the <i> filter() and concat() methods </i> to compute the union of two arrays in JavaScript </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      let cities1 = ["Surat", "Ahmedabad", "Rajkot", "Vadodara", "Pune"];
      let cities2 = ["Mumbai", "Pune", "Nagpur", "Nashik", "Rajkot"];
      let cities = cities1.concat(cities2);
      cities.sort();
      
      // filter unique values in the array
      let unionCities = cities.filter((value, index) => cities.indexOf(value) === index);
      output.innerHTML = "First array: " + JSON.stringify(cities1) + "<br>";
      output.innerHTML += "Second array: " + JSON.stringify(cities2) + "<br>";
      output.innerHTML += "The union of the two arrays: " + JSON.stringify(unionCities) + "<br>";
   </script>
</body>
</html>

结论

用户学习了在 JavaScript 中计算数组并​​集的三种不同方法。第一种方法需要使用集合数据结构的线性代码。第二种方法使用对象,第三种方法使用 filter() 和 concat() 方法。

以上是如何计算 JavaScript 数组的并集?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除