node.js 在 EJS 中循环 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22952044/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:07:34  来源:igfitidea点击:

Loop through JSON in EJS

node.jsejs

提问by Hammer

I have codes in EJS below,

我在下面的 EJS 中有代码,

<script>
    var row =<%-JSON.stringify(data)%>
    console.log(row);
</script>
<% for(var i=0; i<JSON.stringify(data).length; i++) {%>
   <tr>
     <td>
       <%= JSON.stringify(data)[i].id%>
     </td>
   </tr>
<% } %>

output of row is correct, an array of 3 objects, each with properties id, name etc.. I can manipulate the row to popuate the table in JS. However, I am wonderring whether there is a way to allow it be done in the above manner?

行的输出是正确的,一个由 3 个对象组成的数组,每个对象都有属性 id、名称等。我可以操纵行以在 JS 中填充表。但是,我想知道是否有办法允许以上述方式完成?

When I run the code above, JSON.stringify(data).length is not 3, but rather the length of the whole string.

当我运行上面的代码时,JSON.stringify(data).length 不是 3,而是整个字符串的长度。

Another questions is when I try to add

另一个问题是当我尝试添加

<% alert('t'); %> or <% window.alert('t'); %>, it gives me 'not defined' error...

<% alert('t'); %> 或 <% window.alert('t'); %>,它给了我“未定义”错误...

Helps appreciated.

帮助赞赏。

Regards Hammer

问候锤子

回答by rossipedia

JSON.stringifyreturns a String. So, for example:

JSON.stringify返回一个String. 因此,例如:

var data = [
    { id: 1, name: "bob" },
    { id: 2, name: "john" },
    { id: 3, name: "jake" },
];

JSON.stringify(data)

will return the equivalent of:

将返回相当于:

"[{\"id\":1,\"name\":\"bob\"},{\"id\":2,\"name\":\"john\"},{\"id\":3,\"name\":\"jake\"}]"

as a Stringvalue.

作为一个String值。

So when you have

所以当你有

<% for(var i=0; i<JSON.stringify(data).length; i++) {%>

what that ends up looking like is:

最终看起来是这样的:

<% for(var i=0; i<"[{\"id\":1,\"name\":\"bob\"},{\"id\":2,\"name\":\"john\"},{\"id\":3,\"name\":\"jake\"}]".length; i++) {%>

which is probably notwhat you want. What you probably dowant is something like this:

这可能不是你想要的。什么你可能做的想是这样的:

<table>
<% for(var i=0; i < data.length; i++) { %>
   <tr>
     <td><%= data[i].id %></td>
     <td><%= data[i].name %></td>
   </tr>
<% } %>
</table>

This will output the following table (using the example datafrom above):

这将输出下表(使用data上面的示例):

<table>
  <tr>
    <td>1</td>
    <td>bob</td>
  </tr>
  <tr>
    <td>2</td>
    <td>john</td>
  </tr>
  <tr>
    <td>3</td>
    <td>jake</td>
  </tr>
</table>

回答by Parsa

JSON.stringify(data).length return string length not Object length, you can use Object.keys.

JSON.stringify(data).length 返回字符串长度不是 Object 长度,可以使用 Object.keys。

<% for(var i=0; i < Object.keys(data).length ; i++) {%>

https://stackoverflow.com/a/14379528/3224296

https://stackoverflow.com/a/14379528/3224296

回答by Renish Gotecha

in my case, datasis an objects of Array for more information please Click Here

在我的情况下,datas是 Array 的对象以获取更多信息,请单击此处

<% for(let [index,data] of datas.entries() || []){  %>
 Index : <%=index%>
 Data : <%=data%>
<%} %>