如何在 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>


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

使用 array.some() 方法

array.some() 方法遍歷陣列的每個元素,就像 JavaScript 中的 forEach 迴圈一樣。它將 callback 函式作為引數。可以使用定義一些條件來應用於 callback 函式內的每個陣列元素。即使條件對單個元素變為 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() 方法將回調函式作為引數。

  • callback function − 它是一個函式,用於檢查每個元素的一些條件。如果任何單個元素評估條件為 true,則 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-07-20

5K+ 瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

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