JavaScript 如何使用包括并检查一个对象的属性的数组

JavaScript 如何使用包括并检查一个对象的属性的数组

我们的任务是检查数组是否包含一个特定的值。此外,我们还需要检查数组是否包含具有给定属性的特定对象。

本教程将使用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()方法,并使用’in’操作符检查数组中是否存在任何包含作为checkProperties()函数参数传递的属性的对象。此外,我们用不同的参数值在按钮点击事件中调用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()方法来检查数组中是否至少包含一个具有特定属性的对象。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

JavaScript 教程