Skip to content

JavaScript print array of objects | Example code

  • by

Simply stringify the object and assign it to the innerHTML or use the console log to print an array of objects in JavaScript.

yourContainer.innerHTML = JSON.stringify(data);

If you want something prettier, do

yourContainer.innerHTML = JSON.stringify(data, null, 4);

Source: stackoverflow.com

Example print array of objects in JavaScript

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <pre id="data"></pre>
  <script>

   var lineChartData = [{
    date: new Date(2009, 10, 2),
    value: 5
  }, {
    date: new Date(2009, 10, 25),
    value: 30
  }, {
    date: new Date(2009, 10, 26),
    value: 72,
    customBullet: "images/redstar.png"
  }];

  document.getElementById("data").innerHTML = JSON.stringify(lineChartData, null, 4);
</script>

</body>
</html>

Output:

JavaScript print array of objects

More example display object array

<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>

Do comment if you have any doubts or suggestions on this JS print Array topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading