如何使用陣列包含和檢查物件相對於物件屬性?
任務是檢查陣列是否包含特定值。此外,我們需要檢查陣列是否包含具有給定屬性的特定物件。
本教程將使用 array.includes() 和 array.some() 方法來檢查陣列是否包含特定值或具有特定屬性的物件。
使用 array.includes() 方法檢查陣列中是否存在值
array.includes() 方法允許我們檢查陣列是否包含任何值。簡單來說,我們可以使用 array.includes() 方法在陣列中搜索值。
語法
使用者可以按照以下語法使用 array.includes() 方法在陣列中搜索值。
array.includes(value, startIndex);
在上面的語法中,陣列包含各種元素,例如字串、數字和布林值。
引數
Value − 這是要在陣列中搜索的值。
startIndex − 這是一個可選引數,用於從 startIndex 開始搜尋。
返回值
它根據值是否存在於陣列中返回布林值。
示例 1
在下面的示例中,我們使用了 array.includes() 方法,沒有傳遞 startIndex 可選引數。因此,它將從第 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() 方法並檢查陣列中是否存在任何包含作為 checkProperties() 函式引數傳遞的屬性的物件,使用“in”運算子。此外,我們在按鈕點選事件上使用不同的引數值呼叫 checkProperties() 函式。
在輸出中,如果任何單個物件包含特定屬性,則得到 true;否則為 false。
<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 屬性,並透過將其值與“undefined”字串值進行比較來檢查它是否存在於物件中。
因此,這是使用 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() 方法來檢查陣列是否至少包含一個具有特定屬性的物件。