对象中的 ejs 数组
ejs array in an object
这是 nodejs(express) 中的代码:
response.redirect('file',{test: [{name:'sarah',arr:[1,2,3]},{name:'beck',arr: [2,3,4]}]
现在我想为 objects.In ejs 文件数组中的每个名称访问 'arr':
<% test.forEach(index,item){ %>
<p><%= item.arr %></p>
<% }) %>
打印为 1,2,3 和 2,3,4,但我希望答案为 [1,2,3] 和 [2,3,4]。有人可以帮忙吗?
您可以使用 for .. of
循环遍历 test
的所有条目,然后对条目的 arr
属性 使用 JSON.stringify
:
<% for(const testData of test){ %>
<%= JSON.stringify(testData.arr) %>
<% } %>
这是 nodejs(express) 中的代码:
response.redirect('file',{test: [{name:'sarah',arr:[1,2,3]},{name:'beck',arr: [2,3,4]}]
现在我想为 objects.In ejs 文件数组中的每个名称访问 'arr':
<% test.forEach(index,item){ %>
<p><%= item.arr %></p>
<% }) %>
打印为 1,2,3 和 2,3,4,但我希望答案为 [1,2,3] 和 [2,3,4]。有人可以帮忙吗?
您可以使用 for .. of
循环遍历 test
的所有条目,然后对条目的 arr
属性 使用 JSON.stringify
:
<% for(const testData of test){ %>
<%= JSON.stringify(testData.arr) %>
<% } %>