[英]iterating over JSON object in ejs partial
I am trying to loop over each entry in "default", in my JSON file below, within an EJS partial.我试图在下面的 JSON 文件中,在 EJS 部分中遍历“默认”中的每个条目。
{
"default": {
"1": {
"id": "alertmessages",
"title": "Your Messages",
"subtitle": "Tasks waiting for your attention",
"image": "alert.png",
},
"2": {
"id": "uploadr",
"title": "Upload",
"subtitle": "Upload",
"image": "baskets.png",
},
etc............
}
}
I am reading this JSON file into my node.js, parsing it and making it available for my dashboard view.我正在将此 JSON 文件读入我的 node.js,解析它并使其可用于我的仪表板视图。 as below
如下
var jsondata = JSON.parse(require('fs').readFileSync('./app/routes/dashboard/buttons.json', 'utf8'));
var content = {
title: 'Dashboard',
user: req.user,
buttonsdata: jsondata
};
res.render('dashboard', content);
My dashboard view, includes an ejs partial in which I am trying to loop over all the objects contained within default (this number may vary).我的仪表板视图包括一个 ejs 部分,我试图在其中循环遍历默认值中包含的所有对象(这个数字可能会有所不同)。 I want to do this without Jquery.
我想在没有 Jquery 的情况下做到这一点。 But having trouble getting any of the suggestions working that I have found on other threads.
但是无法获得我在其他线程上找到的任何建议。
console.log("<%= buttonsdata.default.[1].id %>")
gives me what I would expect, however if i try and console log buttons.length , which is used in loop examples I have seen, it just comes up blank.给了我我所期望的,但是如果我尝试控制台日志按钮.length ,它在我看到的循环示例中使用,它只是空白。 also how can I console log the entire contents of the object to see its structure?
也如何控制台记录对象的全部内容以查看其结构? thirdly - this object, in this manner as I can tell is available through ejs, however not to core javascript and i can only access it using the ejs <% %> tags.
第三 - 这个对象,以我可以告诉的方式通过 ejs 可用,但不是核心 javascript,我只能使用 ejs <% %> 标签访问它。 is this a correct understanding?
这是正确的理解吗?
You can use Object.keys() :您可以使用Object.keys() :
const buttonsdata = { "default": { "1": { "id": "alertmessages", "title": "Your Messages", "subtitle": "Tasks waiting for your attention", "image": "alert.png", }, "2": { "id": "uploadr", "title": "Upload", "subtitle": "Upload", "image": "baskets.png", } } }; Object.keys(buttonsdata.default).forEach(function (key) { console.log(buttonsdata.default[key].id); });
Now, according to your code, in the view should work like this:现在,根据您的代码,在视图中应该像这样工作:
<ul>
<% Object.keys(buttonsdata.default).forEach(function(key) { %>
<li><%= buttonsdata.default[key].id %></li>
<% }) %>
</ul>
Your json contains only objects, it doesn't have any arrays in it, so you won't be able to use buttonsdata.length
您的 json 仅包含对象,其中没有任何数组,因此您将无法使用
buttonsdata.length
Displaying the whole object - display the object from within your controller, not your template.显示整个对象- 从您的控制器中显示对象,而不是您的模板。 Look at this answer to see how
看看这个答案,看看如何
this object is available through ejs - yes, that's a correct understanding - I assume that by core javascript you mean the javascript that's executed in the browser.这个对象可以通过 ejs 获得——是的,这是一个正确的理解——我认为核心 javascript是指在浏览器中执行的 javascript。 The whole ejs template is processed on the backend.
整个ejs模板在后端处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.