首頁  >  文章  >  web前端  >  如何使用包含物件的陣列並根據物件的屬性檢查物件?

如何使用包含物件的陣列並根據物件的屬性檢查物件?

王林
王林轉載
2023-08-24 19:41:02878瀏覽

如何使用包含物件的陣列並根據物件的屬性檢查物件?

任務是檢查陣列是否包含特定值。另外,我們需要檢查陣列是否包含具有給定屬性的特定物件。

本教學將使用 array.includes() 和 array.some() 方法來檢查陣列是否包含具有特定屬性的值或物件。

使用 array.includes() 方法檢查數組中是否存在值

array.includes() 方法讓我們可以檢查陣列是否包含任何值。簡單來說,我們可以使用 array.includes() 方法在陣列中搜尋值。

文法

使用者可以依照下列語法使用 array.includes() 方法在陣列中搜尋值。

array.includes(value, startIndex);

在上面的語法中,陣列包含各種元素,例如字串、數字和布林值。

參數

  • Value - 這是在陣列中搜尋的值。

  • startIndex - 這是一個可選參數,從 startIndex 開始搜尋。

傳回值

它會根據數組中是否存在該值傳回布林值。

範例 1

在下面的範例中,我們使用了 array.includes() 方法,但沒有傳遞 startIndex、option 參數。因此,它將從第 0 個索引開始在數組中搜尋。在輸出中,使用者可以觀察到 array.includes() 方法對於「hello」字串值傳回 true,對於「abcd」字串值傳回 false。

<html>
<body>
   <h2>Using the <i>array.includes()</i> method to check for the existence of the value in the array.</h2>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      let array = ["Hello", 10, "Hi", true, "Welcome", false, 30, 50, 70, false];
      let result = array.includes("Hello");
      output.innerHTML += "Is Hello exist in the array? " + result + "<br/>";
      result = array.includes("abcd");
      output.innerHTML += "Is abcd exist in the array? " + result + "<br/>";
   </script>
</body>
</html>

在上面的方法中,我們學習了檢查數組物件中是否存在值。現在,我們將學習檢查數組中是否存在具有特定屬性的物件。

使用 array.some() 方法檢查數組中是否存在具有特定屬性的物件

array.some() 方法檢查陣列中是否至少有一個元素符合傳遞給回呼函數的特定條件。因此,在回調函數中,我們將檢查任何物件是否包含特定屬性。

文法

使用者可以按照下面的語法使用 array.some() 方法來檢查數組中是否存在具有特定屬性的物件。

let result = objects.some((object) => property in object);

在上面的語法中,我們使用「in」運算子來檢查陣列中所有物件的任何物件中是否存在某個屬性。

範例 2

在下面的範例中,我們建立了一個物件數組,每個物件都包含各種屬性和值。此外,我們還使用了 array.some() 方法,並使用「in」運算子檢查數組中是否存在包含作為 checkProperties() 函數參數傳遞的屬性的物件。此外,我們在按鈕點擊事件上使用不同的參數值來呼叫 checkProperties() 函數。

在輸出中,如果任何單一物件包含特定屬性,我們就會得到 true;否則為假。

<html>
<body>
   <h2>Using the <i>array.some()</i> method to check for the existence of the object with a particular property.</h2>
   <div id = "output"> </div>
   <button onclick = "checkProperties('salary'); checkProperties('id')">  Check for Object </button>
   <script>
      let output = document.getElementById('output');
      function checkProperties(property) {
         let objects = [
            { prop1: "value1", prop2: "Value2", num: 30 },
            { name: "name", prop3: "Value3", salary: 40860 },
            { age: 20, prop2: "Value2", number: 60 },
            { prop1: "value10", prop2: "Value30", num: 90 },
            { prop1: "value20", prop2: "Value40", num: 100 }
         ];
         let isProperty = objects.some((object) => property in object);
         output.innerHTML += "Is objects array contain any object with " + property + " property? " + isProperty + "<br/>";
      }
   </script>
</body>
</html>

範例 3

在下面的範例中,我們對物件陣列使用了 array.reduce() 方法。在reduce()方法的回調函數中,我們存取物件的salary屬性,並透過將其值與「未定義」字串值進行比較來檢查它是否存在於物件中。

因此,這是使用 some() 方法來尋找包含特定屬性的任何物件的另一種方法。

<html>
<body>
   <h2>Using the <i>array.some()</i> method to check for the existence of the object with a particular property.</h2>
   <div id = "output"> </div>
   <button onclick = "checkProperties()"> Check for Object </button>
   <script>
      let output = document.getElementById('output');
      function checkProperties() {
         let objects = [
            { color: "blue", id: "232d", num: 30 },
            { name: "name", prop3: "534", maximum: 10 },
            { age: 20, id: "dred", numValue: 90 },
            { color: "blue", id: "87gd", minimum: 0 },
            { color: "green", id: "56fghfh", num: 100 }
         ];
         let isSalary = objects.some((object) => { object.salary != "undefined" });
         output.innerHTML += "Is objects array contains any object with " + "salary" +    "property? " + isSalary + "<br/>";
      }
   </script>
</body>
</html>

我們使用了 array.includes() 和 array.some() 方法來搜尋陣列中的值和物件。不過,使用者也可以使用 JavaScript 中的 filter() 方法來檢查陣列是否至少包含一個具有特定屬性的物件。

以上是如何使用包含物件的陣列並根據物件的屬性檢查物件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除