我們可以通過合并兩個數組并刪除重復元素來獲得兩個數組的并集,并且并集給出兩個數組中唯一的元素。
在本教程中,我們將學習使用各種方法計算 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 數組的并集?的詳細內容,更多請關注www.92cms.cn其它相關文章!