首頁  >  文章  >  web前端  >  如何計算 JavaScript 陣列的並集?

如何計算 JavaScript 陣列的並集?

王林
王林轉載
2023-09-10 18:49:091133瀏覽

如何计算 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刪除