How to print object array in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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

Example

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

Live Demo

<!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  
   }
]
raja
Published on 03-Feb-2018 10:40:28
Advertisements