JavaScript 如何计算数组的并集

JavaScript 如何计算数组的并集

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

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

使用set()数据结构

第一种方法是使用set()数据结构。set数据结构只能包含唯一元素。我们可以将两个数组的所有元素添加到set中,并从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包含了names1和names2数组的并集。

<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

在下面的示例中,cities1和cities2数组包含一些城市名称,其中一些是相同的。cities数组包含了这两个数组的元素。

然后,我们使用sort()方法对cities数组进行排序。接下来,我们使用filter()方法从cities数组中过滤出唯一的值。在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()方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程