Categories
JavaScript Answers

How to print object array in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *