如何统计 JavaScript 数组对象?


在本教程中,我们将学习如何统计JavaScript 数组对象数组是一种数据结构,它包含在JavaScript中的字符串数字对象等。 对象是一种包含与其相关的属性和方法的实体。我们可以使用对象的引用访问对象的属性并调用对象的方法。

通常,要查找数组长度,我们可以使用array.length() 方法,它返回数组包含的元素总数。但是,如果我们只需要统计对象元素呢?

本教程介绍了多种方法来统计 JavaScript 数组中对象元素的总数。

  • 使用 for 循环和 instanceof 运算符

  • 使用 array.filter() 方法

使用 for 循环和 instanceof 运算符

在这种方法中,我们将使用for 循环来统计数组中对象的总数。用户可以遍历数组的每个元素,并使用instanceof 运算符检查每个元素的类型。他们可以将长度变量初始化为 0 来存储对象的总数。在遍历数组元素时,如果他们找到任何对象类型的实体,他们可以将长度加 1。

语法

用户可以使用以下语法使用 for 循环和 instanceof 运算符来统计数组对象。

let objectsLen = 0;
for (let i = 0; i < myArr.length; i++) {

   // if entity is object, increase objectsLen by 1, which is the stores the total number of objects in array.
   if (myArr[i] instanceof Object) {
      objectsLen++;
   }
}

算法

  • 步骤 1 − 创建一个名为 objectsLen 的变量并将其初始化为 0。

  • 步骤 2 − 创建一个数组并添加一些对象和其他元素。

  • 步骤 3 − 为了统计数组中对象的个数,我们使用 for 循环遍历数组,并使用 instanceof 运算符检查元素类型。

  • 步骤 4 − 如果我们找到对象类型的实体,我们将向 objectsLen 变量添加 1。

  • 步骤 5 − 最后,我们将打印 objectsLen 变量,它代表对象的总数。

示例

在下面的示例中,统计数组中对象的总数。我们使用for 循环遍历数组,并应用instanceof 运算符来检查数组元素的类型。

<html>
   <head>
   </head>
   <body>
      <h2> Count JavaScript array objects. </h2>
      <h4> Counting total number of array objects in array using the <i> for loop. </i> </h4>
      <p id = "objects"> </p>
      <script>
         let objects = document.getElementById("objects");
         var objectsLen = 0;
         
        // array of objects and ohter elements
        var myArr = [{ "id": 1, "int": 10 }, { "id": 2, "int": 20 }, { "id": 3, "int": 30 }, "TutorialsPoint", 20, "Hello"];
        for (let i = 0; i < myArr.length; i++) {
           // checking the type of the object.
           if ( myArr[i] instanceof Object ) {
               objectsLen++;
           }
       }
       objects.innerHTML += "Original Array: " + JSON.stringify(myArr)+"<br>";
       objects.innerHTML += "<br>Total number of objects: " +  objectsLen ;
   </script>
   </body>
</html>

使用 array.filter() 方法

在 JavaScript 中,array.filter() 方法用于过滤数组的元素。用户可以向方法中添加回调函数,并在回调函数中添加一些过滤条件。在这种情况下,用于过滤所有对象的过滤条件是检查元素类型。用户可以使用typeof 运算符检查对象的类型,如果实体是对象类型,则从回调函数返回 true。否则,返回 false。

array.filter() 方法返回所有过滤值的数组。因此,我们将获得所有对象的数组,并且可以使用.length() 方法测量其长度。

语法

用户可以按照以下语法使用 array.filter() 方法来统计对象的个数。

// filter all entity which type is object
let allObject = array.filter((val) => {

   // checking the type of elements using the typeof operator.
   if ( typeofval == 'object' ) {
      return true;
   } else {
		return false;
   }
});
LettotalObjects = allObject.length;

参数

  • array − 它是一个包含对象实体和其他元素的数组。

  • val − 它是数组中的一个元素,用户希望检查其类型,如果实体是对象,则将其过滤。

示例

在下面的示例中,我们使用了array.filter() 方法从给定数组中过滤所有对象。最后,我们计算了由array.filter() 方法返回的对象数组的长度。

<html>
   <body>
   <h2> Count JavaScript array objects. </h2>
   <h4> Counting total number of array objects in array using the <i> arrays.filter() </i> method. </h4>
   <p id = "objects"> </p>
   <script>
      let objects = document.getElementById("objects");
      
      // array of objects and ohter elements      
      var array = ["TutorialsPoint", 20, { "id": 2, "int": 20 }, { "id": 3, "int": 30 }, "Hello"];
      let allObject = array.filter((val) => {
         if ( typeof val == 'object' ) {
            return true;
         } else {
            return false;
         }
      });
       let objectsLen = allObject.length;
       objects.innerHTML += "Original String: " + JSON.stringify(array) + " <br> ";
       objects.innerHTML += "<br>Total Objects in the array: "  + objectsLen 
   </script>
   </body>
</html>

用户已经学习了如何在 JavaScript 数组中计算对象实体。第一种方法和第二种方法的时间复杂度相同。当我们查看两种方法的空间复杂度时,第一种方法的空间复杂度更低,并且优化得更好。

更新于: 2023年10月31日

25K+ 次浏览

启动您的职业生涯

通过完成课程获得认证

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