如何使用 JavaScript 檢查一個數組是否為另一個數組的子集?


如果第二個陣列包含第一個陣列的所有元素,則第一個陣列是第二個陣列的子集。因此,有時我們可能需要檢查一個數組是否為另一個數組的子集。

在本教程中,我們將學習使用三種不同的方法來檢查一個數組是否為另一個數組的子集。

使用 for 迴圈和 array.includes() 方法

使用者可以使用 for 迴圈遍歷第一個陣列的每個元素。之後,他們可以使用 includes() 方法檢查第二個陣列是否包含第一個陣列的每個元素。

如果第二個陣列包含第一個陣列的所有元素,則第一個陣列是第二個陣列的子集。

語法

使用者可以按照以下語法使用 for 迴圈和 includes() 方法來確定一個數組是否為另一個數組的子集。

for (let ele of array1) {
   if (!array2.includes(ele)) {
      return false;
   }
}

在上面的語法中,我們檢查 array1 是否為 array2 的子集。

演算法

  • 步驟 1 − 我們將檢查 array1 是否為 array2 的子集。

  • 步驟 2 − 使用 for-of 迴圈遍歷陣列的每個元素。

  • 步驟 3 − 使用 array.includes() 方法檢查 array1 的每個元素是否包含在 array2 中。

  • 步驟 4 − 如果 array1 中的任何一個元素都不包含在 array2 中,則返回 false。

  • 步驟 5 − 如果 array2 包含 array1 的所有元素,則 for 迴圈迭代將成功並返回 true。

示例

我們在下面的示例中建立了三個包含不同數字值的陣列。我們建立了 isSubset() 函式,它將兩個陣列作為引數。該函式檢查 array1 是否為 array2 的子集,並根據該結果返回布林值。

我們正在檢查 array2 和 array3 是否為 array1 的子集。使用者可以在輸出中觀察結果。

<html>
<body>
   <h3>Using the <i>for loop and includes() method</i> to determine if one array is a subset of another array.</h3>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
      let array1 = [10, 20, 30, 40, 50, 60, 70, 80, 90];
      let array2 = [20, 30, 70, 80];
      let array3 = [20, 43, 45];
      function isSubset(array1, array2) {
         // Iterating through all the elements of array1
         for (let ele of array1) {
            // check if array2 contains the element of array1
            if (!array2.includes(ele)) {
               output.innerHTML += "The " + array1 + " is not a subset of " + array2 + "<br>";
               return false;
            }
         }
         output.innerHTML += "The " + array1 + " is a subset of " + array2 + "<br>";
         // If array1 contains all elements of array2 return true
         return true;
      }
      isSubset(array2, array1);
      isSubset(array3, array1)
   </script>
</body>
</html>

使用 array.some() 和 array.indexOf() 方法

array.some() 方法將回調函式作為引數,該函式根據參考陣列中至少一個滿足條件的元素返回布林值。

array.indexOf() 方法如果元素存在於陣列中則返回元素的索引;否則,它返回 -1。因此,如果我們在第一個陣列中找到任何元素,其在第二個陣列中的索引為 -1,則表示第一個陣列不是第二個陣列的子集。

語法

使用者可以按照以下語法使用 array.some() 和 array.indexOf() 方法來檢查一個數組是否為另一個數組的子集。

let isSubset = !data2.some((string) => data1.indexOf(string) == -1);

在上面的語法中,如果 some() 方法返回 true,則 data1 陣列不是 data2 的子集。因此,我們將它的相反布林值儲存在 isSubset 變數中。

示例

下面的示例包含兩個字串陣列,並檢查 data1 陣列是否為 data2 陣列的子集。data1 陣列包含 data2 的所有元素。因此,使用者可以在輸出中看到它表示 data2 陣列是 data1 的子集。

<html>
<body>
   <h3>Using the <i>array.some() and array.indexOf() method</i> to check if one array is a subset of another.</h3>
   <p id="output"></p>
   <script>
      let output = document.getElementById("output");
      let data1 = ["Hello", "Hi", "Users"];
      let data2 = ["Hello", "Users"];
      let isSubset = !data2.some((string) => data1.indexOf(string) == -1);
      if (isSubset) {
         output.innerHTML += "The " + data2 + " is a subset of " + data1 + " array. <br>";
      } else {
         output.innerHTML += "The " + data2 + " is not a subset of " + data1 + " array. <br>";
      }
   </script>
</body>
</html>

使用 array.every() 方法和 set()

array.every() 方法如果每個元素都滿足回撥函式返回的條件,則返回 true。

我們可以建立所有陣列元素的 set(),因為 set 包含唯一的陣列元素。

語法

按照以下語法使用 set 和 every() 方法。

let setOfArray = new Set(num1);
let result = num2.every(num => setOfArray.has(num));

示例

在下面的示例中,我們建立了 num1 陣列所有元素的 set。之後,我們使用 javascript set 的 has() 方法檢查該 set 是否包含 num2 陣列的每個元素。

<html>
<body>
   <h3>Using the <i>array.every() method and set</i> to check if one array is a subset of another array.</h3>
   <p id="output"></p>
   <button onclick="checkForSubset()">Check for subset</button>
   <script>
      let output = document.getElementById("output");
      let num1 = [45, 65, 45, true, false, 45, 43, 32];
      let num2 = [false, true, false, true];
      function checkForSubset() {
         // create a set of the parent array
         let setOfArray = new Set(num1);
         // Check if every element of the child array is in the set of the parent array
         let result = num2.every(num => setOfArray.has(num));
         if (result) {
            output.innerHTML += "The " + num2 + " is a subset of " + num1 + " array. <br>";
         } else {
            output.innerHTML += "The " + num2 + " is not a subset of " + num1 + " array. <br>";
         }
      }
   </script>
</body>
</html>

更新於: 2023年1月19日

9K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告