如何在 JavaScript 中检查数组是否包含对象?

front end technologyjavascriptweb development

在本教程中,我们将学习检查数组是否包含对象。实际数组包含具有相同数据类型的值。在 JavaScript 中,数组被列表替换,包括任何数据类型的值,即使用户可以用数字和字符串在列表内添加对象。

因此,程序员有时需要弄清楚数组是否包含对象。如果数组包含对象,程序员可以访问对象属性并执行某些操作。

在这里,我们有不同的方法来检查数组是否包含对象。

  • 使用 array.includes() 方法

  • 使用 array.some() 方法

使用 array.includes() 方法

Array.includes() 方法在 JavaScript 中当我们需要检查数组是否包含列表中的特定值时最有用。我们可以使用此方法找到任何特定的对象实体。我们将传递一个对象作为 array.includes() 方法的参数,如果数组包含具有相同键和值的对象,则返回 true。

语法

用户可以按照以下语法使用 array.includes() 方法。

let array = [ "TutorialsPoint", "Hello", object, "World" ];
let result = array.includes( object , Starting_position );

参数

array.includes() 方法包含两个参数。

  • object −它是您想要在数组中查找的对象的实体类型。

  • starting_position − 它是数组中基于 0 索引的起始位置,用户希望从该位置开始在数组中查找对象。

示例

在下面的示例中,我们创建了一个字符串的单个对象和数组。此外,它还包含数组内的对象。我们使用 array.includes() 方法从各个位置开始查找数组中的对象。

<html>
<head>
   <title>Check if array includes object in JavaScript.</title>
</head>
<body>
   <h3>Check if array includes object in JavaScript.</h3
   <p>Using Array.includes() method to check existence of the object from starting position 1:</p>
   <div id="existance1"></div>
   <p>Using Array.includes() method to check existence of the object from starting position 3:</p>
   <div id="existance2"></div>
   <script>
      let existance1 = document.getElementById("existance1");
      let existance2 = document.getElementById("existance2");
      let object = { "name": "TutorialPoints", "website": "TutorialsPoint.com" };
      let array = [ "TutorialsPoint", "Hello", object, "World" ];
      let result = array.includes(object, 1); // we are checking from the starting position 1 instead of zero.
      existance1.innerHTML = result;
      result = array.includes(object, 3);
      existance2.innerHTML = result;
   </script>
</body>
</html>


在上面的示例代码中,对象的位置是 2,因为列表从索引 0 开始。如果我们从 1 开始查找对象位置,我们将捕获该对象并返回 true。当我们从第 3 个位置开始查找对象时,它返回 false。

使用 array.some() 方法

array.some() 方法遍历数组的每个元素,就像 JavaScript 中的 forEach 循环一样。它以 callback 函数作为参数。用户可以定义一些条件以应用于 callback 函数内的每个数组元素。即使条件对于单个元素变为真,它也会返回 true。在我们的例子中,我们将检查值的类型,如果它是一个对象,它将返回 true。

语法

array.some() 方法的语法如下所示。

let array = [ "TutorialsPoint", "Hello", object, "World" ]
let result = array.some( function callback(item) {
   // 回调函数的条件。
   return typeof item == "object";
});

此外,用户可以在回调函数中使用箭头函数。在这里,我们创建了命名函数以保持代码的简单性。

参数

array.some() 方法将回调函数作为参数。

  • 回调函数 − 它是一个检查每个元素的某些条件的函数。如果任何单个元素评估条件为真,array.some() 方法将返回 true。

示例

以下示例演示了 array.some() 方法。我们创建了包含对象的数组。我们将遍历数组的每个元素以检查其类型。如果数组包含任何单个实体,其类型为对象,array.some() 将返回 true。

<html>
<body>
   <h3>Check if array includes object in JavaScript.</h3>
   <p>Using some() method to check existence of the object in the array:</p>
   <div id="existance1"></div>
   <script>
      let existance1 = document.getElementById("existance1");
      let existance2 = document.getElementById("existance2");
      let obj = { "name": "TutorialPoints", "website": "TutorialsPoint.com" };
      let arr = [ "TutorialsPoint", "Hello", obj, "World" ];
      let result = arr.some( function myfunction(item) {
         // checking the type of every item and if it is object it returns true.
         let boolean = typeof item == "object";
         return boolean;
      });
      existance1.innerHTML = result;
   </script>
</body>
</html>


结论

在本教程中,我们学习了两种检查数组(包括对象)的方法。第一种方法检查数组中是否存在具有相同键值对的对象。第二种方法不是检查具有相同键值对的对象,而是检查对象类型的实体。

但是,JavaScript 中还有许多其他方法可以检查数组中对象实体的存在。用户可以使用 find() 方法,其工作原理类似于 array.some() 方法。此外,array,filter()array.indexof() 方法也可用于解决本教程中给出的问题。


相关文章