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


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

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

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

  • 使用 array.includes() 方法

  • 使用 array.some() 方法

使用 array.includes() 方法

Array.includes() 方法在我们需要检查数组是否包含列表中的特定值时非常有用。我们可以使用此方法找到任何特定的对象实体。我们将把一个对象作为 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>


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

使用 array.some() 方法

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

语法

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

let array = [ "TutorialsPoint", "Hello", object, "World" ]
let result = array.some( function callback(item) {
   // condition of the callback function.
   return typeof item == "object";
});

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

参数

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

  • callback function − 它是一个函数,用于检查每个元素的一些条件。如果任何单个元素评估条件为 true,则 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() 方法也可用于解决本教程中给出的问题。

更新于: 2022-07-20

5K+ 浏览量

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.