在javascript中显示实际的[object Object] [英] show the actual [object Object] in javascript

查看:49
本文介绍了在javascript中显示实际的[object Object]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象的对象,我想遍历这些对象并在浏览器中显示它们;我有以下代码,但仅向我显示了[object Object] [object Object] [object Object];如何显示实际对象?

I have an object of objects and I want to iterate through the objects and show them in the browser; I have the following code, but it just shows me [object Object][object Object][object Object]; how can I show the actual objects?

my my_obj looks like:

{
    "User": {
        "description": "A user."
    },
    "Media": {
        "description": "A media ."
    }
}

var output = "";
for (var key in my_obj) {
    output += my_obj[key];
}
response.send(output);

谢谢

推荐答案

看起来这个问题本质上是重复的你的.

It looks like this question is essentially a duplicate of yours.

该问题的接受的答案使用

The accepted answer for that question uses the console object to print the contents of the object to the JavaScript debugging console in the browser's developer tools (usually accessible with F12). You can typically interact with the object, expanding and collapsing its properties, in the logged output in the console.

console.log(my_obj);

但是,这并不提供一种将对象的内容打印到网页上的简便方法.为此,您可以按照上面链接的问题的投票最高的答案,该问题使用

However, this doesn't provide an easy way to print the contents of the object to the webpage. For that, you can follow the above-linked question's highest-voted answer, which uses the JSON object, specifically it's stringify method.

var my_obj_str = JSON.stringify(my_obj);

此方法返回对象的字符串化版本(即,它将看起来像对象文字),然后可以像上面那样将其记录到控制台(尽管它将是字符串,而不是交互式对象!),或者以任意方式将字符串放入网页内容中.

This method returns a stringified version of your object (i.e. it will appear like an object literal), which can then either be logged to the console like above (although it would be a string, not an interactive object!), or put into the webpage in whatever manner you like putting strings into webpage content.

这篇关于在javascript中显示实际的[object Object]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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