To print object array in JavaScript, we call the JSON.stringify
method.
For instance, we write
const lineChartData = [
{
date: new Date(2022, 10, 2),
value: 5,
},
{
date: new Date(2022, 10, 25),
value: 30,
},
{
date: new Date(2022, 10, 26),
value: 72,
customBullet: "images/redstar.png",
},
];
document.getElementById("whereToPrint").innerHTML = JSON.stringify(
lineChartData,
null,
2
);
to call JSON.stringify
to convert the lineChartData
array into a JSON string with each level indented with 2 spaces.
Then we set that as the value of the element’s innerHTML
property to render the JSON string in the element as its content.