javascript 在javascript中显示实际的[object Object]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25042024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
show the actual [object Object] in javascript
提问by ajp15243
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?
我有一个对象对象,我想遍历这些对象并在浏览器中显示它们;我有以下代码,但它只显示我 [object Object][object Object][object Object]; 如何显示实际对象?
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);
Thanks
谢谢
回答by ajp15243
It looks like this questionis essentially a duplicate of yours.
看起来这个问题本质上是你的重复。
The accepted answer for that questionuses the console
objectto 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
对象将对象的内容打印到浏览器开发人员工具中的 JavaScript 调试控制台(通常可通过 F12 访问)。您通常可以在控制台的记录输出中与对象交互,展开和折叠其属性。
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.
但是,这并没有提供将对象的内容打印到网页的简单方法。为此,您可以关注上面链接问题的最高投票答案,该答案使用JSON
object,特别是它的stringify
方法。
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.
此方法返回对象的字符串化版本(即它看起来像一个对象文字),然后可以像上面一样将其记录到控制台(尽管它是一个字符串,而不是一个交互式对象!),或者放入网页以您喜欢的任何方式将字符串放入网页内容中。
回答by user1441287
You might need to use JSON.stringify() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
您可能需要使用 JSON.stringify() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify