简体   繁体   English

for循环返回[object Object]

[英]for in loop returns [object Object]

I want to look through an object but I am not sure what it actually contains, and when I loop as follows I get the following results: 我想浏览一个对象,但不确定该对象实际包含什么,当我如下循环时,得到以下结果:

<% for(var index in errors) {%>
    <%= errors[index] %>
<% } %>

[object Object] [object Object] [object Object] [object Object] [object Object]

I DO expect to get back 5 results, but I need to somehow delve deeper into the loop and get the actual content of the objects inside, but I do not know how to do that? 我确实希望获得5个结果,但是我需要以某种方式深入研究循环并获取对象内部的实际内容,但是我不知道该怎么做?

This is all plain javascript, the script above is just a templating engine for node/express. 这都是纯JavaScript,上面的脚本只是node / express的模板引擎。

stringify your objects before printing: 在打印之前对对象进行stringify

<% for(var index in errors) {%>
    <%= JSON.stringify(errors[index]) %>
<% } %>

The error object might be like, error={ {},{},{} } The format <%= %> , only prints the string value not the object literals, If you want to debug, you can write JSON.stringify(errors[index]) . 错误对象可能类似于error={ {},{},{} }格式<%=%> ,仅显示字符串值,而不显示对象文字,如果要调试,可以编写JSON.stringify( errors [index])

Or if you want to print the child elements use error[index]['childKeyName'] 或者,如果您要打印子元素,请使用error [index] ['childKeyName']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM