仅使用 EJS 迭代 json 键

1N5*_*818 4 json key-value ejs node.js

仅使用 EJS(在 NodeJS 的 .ejs 文件中的 EJS 模板中),我将如何迭代 JSON 对象(不是数组)的键?

我正在寻找这样的东西:

像这样的 JSON 对象:

the_object = {
    1: "belongs to id 1",
    3: "belongs to id 3",
    12: "belongs to id 12"
}
Run Code Online (Sandbox Code Playgroud)

一些 EJS 模板如下:

<% for (key, value: the_object) { %>
    <li>the key is: <%= key %></li>
<% } %>
Run Code Online (Sandbox Code Playgroud)

产生类似的东西:

  • 关键是:1
  • 关键是:3
  • 关键是:12
  • 这可能吗?

    小智 5

       <%  Object.keys(the_object).forEach(function (key) { %>
        <li>the key is: <%= key %></li>
       <% }) %>
    
    Run Code Online (Sandbox Code Playgroud)