如何在 JavaScript 中打印对象数组? [英] How to print object array in JavaScript?

查看:104
本文介绍了如何在 JavaScript 中打印对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JavaScript 中创建了一个对象数组.如何在浏览器窗口中打印对象数组,类似于 PHP 中的 print_r 函数?

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"
        }];

推荐答案

Simply stringify 您的对象并将其分配给 innerHTML.

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>

但如果你只是为了调试而这样做,那么你最好使用带有 console.log(lineChartData) 的控制台.

But if you just do this in order to debug, then you'd better use the console with console.log(lineChartData).

这篇关于如何在 JavaScript 中打印对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆