如何在JavaScript中檢查陣列是否包含物件?


在本教程中,我們將學習如何檢查陣列是否包含物件。實際陣列包含相同資料型別的數值。在JavaScript中,陣列被列表取代,列表包含任何資料型別的數值,即使使用者可以在列表中新增數字和字串型別的物件。

因此,程式設計師有時需要弄清楚陣列是否包含物件。如果陣列包含物件,程式設計師可以訪問物件的屬性並執行某些操作。

這裡,我們有不同的方法來檢查陣列是否包含物件。

  • 使用array.includes()方法

  • 使用array.some()方法

使用array.includes()方法

**Array.includes()** 方法在我們需要檢查陣列是否包含列表中的特定值時非常有用。我們可以使用此方法查詢任何特定的物件實體。我們將把物件作為array.includes() 方法的引數,如果陣列包含具有相同鍵和值的同一物件,則返回true。

語法

使用者可以按照以下語法使用array.includes() 方法。

let array = [ "TutorialsPoint", "Hello", object, "World" ];
let result = array.includes( object , starting_position );

引數

array.includes() 方法包含兩個引數。

  • object − 這是您想要在陣列中查詢的物件的實體型別。

  • starting_position − 這是陣列中基於0索引的起始位置,使用者想要從該位置開始在陣列中查詢物件。

示例

在下面的示例中,我們建立了一個物件和一個字串陣列。此外,它還在陣列中包含了物件。我們使用array.includes() 方法從不同的位置開始查詢陣列中的物件。

<html>
<head>
   <title>Check if array includes object in JavaScript.</title>
</head>
<body>
   <h3>Check if array includes object in JavaScript.</h3
   <p>Using Array.includes() method to check existence of the object from starting position 1:</p>
   <div id="existance1"></div>
   <p>Using Array.includes() method to check existence of the object from starting position 3:</p>
   <div id="existance2"></div>
   <script>
      let existance1 = document.getElementById("existance1");
      let existance2 = document.getElementById("existance2");
      let object = { "name": "TutorialPoints", "website": "TutorialsPoint.com" };
      let array = [ "TutorialsPoint", "Hello", object, "World" ];
      let result = array.includes(object, 1); // we are checking from the starting position 1 instead of zero.
      existance1.innerHTML = result;
      result = array.includes(object, 3);
      existance2.innerHTML = result;
   </script>
</body>
</html>


在上面的示例程式碼中,物件的索引位置是2(列表從索引0開始)。如果我們從索引1開始查詢物件的位置,我們將找到該物件並返回true。當我們從第3個位置開始查詢物件時,它將返回false。

使用array.some()方法

**array.some()** 方法像JavaScript中的forEach迴圈一樣迭代陣列的每個元素。它將回撥函式作為引數。使用者可以在回撥函式內定義一些條件應用於每個陣列元素。即使條件對於單個元素變為true,它也會返回true。在我們的例子中,我們將檢查值的型別,如果它是物件,它將返回true。

語法

array.some() 方法的語法如下所示。

let array = [ "TutorialsPoint", "Hello", object, "World" ]
let result = array.some( function callback(item) {
   // condition of the callback function.
   return typeof item == "object";
});

此外,使用者可以在回撥函式中使用箭頭函式。這裡,我們建立了一個具名函式以保持程式碼的簡單性。

引數

array.some() 方法將回調函式作為引數。

  • 回撥函式 − 它是一個對每個元素檢查某些條件的函式。如果任何單個元素使條件為真,則array.some() 方法返回true。

示例

下面的示例演示了array.some() 方法。我們建立了一個包含物件的陣列。我們將迭代陣列的每個元素以檢查其型別。如果陣列包含任何單個實體,其型別為物件,則array.some() 返回true。

<html>
<body>
   <h3>Check if array includes object in JavaScript.</h3>
   <p>Using some() method to check existence of the object in the array:</p>
   <div id="existance1"></div>
   <script>
      let existance1 = document.getElementById("existance1");
      let existance2 = document.getElementById("existance2");
      let obj = { "name": "TutorialPoints", "website": "TutorialsPoint.com" };
      let arr = [ "TutorialsPoint", "Hello", obj, "World" ];
      let result = arr.some( function myfunction(item) {
         // checking the type of every item and if it is object it returns true.
         let boolean = typeof item == "object";
         return boolean;
      });
      existance1.innerHTML = result;
   </script>
</body>
</html>


結論

在本教程中,我們學習了兩種檢查陣列是否包含物件的方法。第一種方法檢查陣列中是否存在具有相同鍵值對的物件。第二種方法不是檢查具有相同鍵值對的物件,而是檢查物件型別的實體。

但是,JavaScript中還有許多其他方法可以檢查陣列中物件實體的存在。使用者可以使用find() 方法,其工作方式與array.some() 方法類似。此外,array.filter()array.indexOf() 方法也可用於解決本教程中給出的問題。

更新於:2022年7月20日

5K+ 瀏覽量

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.