How to count JavaScript array objects?

JavascriptWeb DevelopmentFront End Technology

To count JavaScript array objects, we’re using Object.prototype.__proto__ property.

Example

You can try to run the following code to count array objects −

Live Demo

<html>
   <head>
      <title>JavaScript Clone Date</title>
   </head>
   <body>
      <script>
         var len = 0;
         var myArr = [{"Hello World!":"Welcome!","int":10},{"Learn":"Share","int":20},{"Tutorials":"Quiz","int":30}];
         myArr.forEach(function(val){
            if(!val.__proto__.__proto__){
               len++;
            }
         });
         document.write("Total Objects: "+len);
      </script>
   </body>
</html>

Output

Total Objects: 3
raja
Published on 14-Feb-2018 06:54:34
Advertisements