如何計算JavaScript陣列的並集?


我們可以透過合併兩個陣列並去除重複元素來獲得兩個陣列的並集,並集包含來自兩個陣列的唯一元素。

在本教程中,我們將學習使用各種方法來計算JavaScript陣列的並集。

使用Set()資料結構

第一種方法是使用Set()資料結構。Set資料結構只能包含唯一元素。我們可以將兩個陣列的所有元素新增到Set中,並從Set的元素建立一個新陣列來建立並集。

語法

使用者可以按照以下語法使用Set資料結構計算JavaScript陣列的並集。

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

在上面的語法中,我們使用了擴充套件運算子連線兩個陣列,並從結果陣列建立一個新的Set。之後,我們再次使用擴充套件運算子將Set的元素新增到陣列中。

示例1

在下面的示例中,array1和array2包含一些公共數字。首先,我們使用擴充套件運算子連線array1和array2。之後,我們使用new Set()建構函式從結果陣列建立一個Set。Set只能包含唯一元素。因此,它包含兩個陣列並集中的所有元素。

之後,我們使用擴充套件運算子將Set的所有元素新增到union陣列中。在輸出中,使用者可以看到兩個陣列的並集。

<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中陣列的並集。第一種方法使用Set資料結構,只需要一行線性程式碼。第二種方法使用物件,第三種方法使用filter()和concat()方法。

更新於:2023年4月19日

495 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告