如何使用包含对象的数组并根据对象的属性检查对象?

任务是检查数组是否包含特定值。另外,我们需要检查数组是否包含具有给定属性的特定对象。
使用 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() 方法来检查数组是否至少包含一个具有特定属性的对象。
javascript