Computer >> Computer tutorials >  >> Programming >> Javascript

How to print object array in JavaScript?


To print object array in JavaScript, use JSON.stringify() method.

Example

You can try to run the following code to display object array −

<!DOCTYPE html>
<html>
   <body>
      <pre id="test"></pre>
      <script>
         var data = [{
            date: new Date(2017, 11, 25),
            value: 10
         }, {
            date: new Date(2017, 11, 30),
            value: 20
         }];
         document.getElementById('test').innerHTML = JSON.stringify(data, null, 4);
      </script>
   </body>
</html>

Output

[    
   {        
      "date": "2017-12-24T18:30:00.000Z",
      "value": 10
   },    
   {        
       "date": "2017-12-29T18:30:00.000Z",  
       "value": 20  
   }
]