I have created an object array in JavaScript. How can I print the object array in the browser window, similar to print_r
function in PHP?
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" }];
To print an array of objects properly, you need to format the array as a JSON string using JSON. stringify() method and attach the string to a <pre> tag in your HTML page. And that's how you can print JavaScript array elements to the web page.
stringify() method is used to print the JavaScript object. JSON. stringify() Method: The JSON. stringify() method is used to allow to take a JavaScript object or Array and create a JSON string out of it.
You can just use the following syntax and the object will be fully shown in the console: console. log('object evt: %O', object);
Simply stringify
your object and assign it to the innerHTML of an element of your choice.
yourContainer.innerHTML = JSON.stringify(lineChartData);
If you want something prettier, do
yourContainer.innerHTML = JSON.stringify(lineChartData, null, 4);
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("whereToPrint").innerHTML = JSON.stringify(lineChartData, null, 4);
<pre id="whereToPrint"></pre>
But if you just do this in order to debug, then you'd better use the console with console.log(lineChartData)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With