如何使用数组包含和检查对象相对于对象属性?
任务是检查数组是否包含特定值。此外,我们需要检查数组是否包含具有给定属性的特定对象。
本教程将使用 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() 方法来检查数组是否至少包含一个具有特定属性的对象。
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP