在模板中渲染嵌套键

时间:2019-04-17 17:51:47

标签: javascript node.js pug

我有一个要通过PUG在前端渲染的分组数组,事实证明这有些棘手

这是数组

{
    "Apr 14th 19": {
        "5:00 PM": [
            {
                "name": "John",
                "message": "Hey there"
            },
            {
                "name": "Josh",
                "message": "Hey"
            }
        ]
    },
    "Apr 15th 19": {
        "5:00 PM": [
            {
                "name": "Jake",
                "message": "Hey you"
            }
        ]
    }
    }

这是我试图用来渲染我想要的模板的帕格代码(以下是所需的输出)

each day in Data
    each hour in day
        each entry in hour
            h2= "The date is" + Object.keys(day)
            h2= "The time is" + Object.keys(hour)
            h2= "The message is" + entry.message 

我想要的模板输出

The date is Apr 14th 19
The time is 5:00 pm
The message is : Hey
The message is : Hey you
(Both messages here because those are 2 nested under under the times)

所有这些都不起作用,我真的可以使用一些帮助

1 个答案:

答案 0 :(得分:0)

您将需要在Pug模板中按照以下步骤进行操作:

each dayObj, day in Data
    h2= "The date is " + day
    each hourObj, hour in dayObj
        h2= "The time is " + hour
        each entry in hourObj
            h2= "The message is : " + entry.message

这是一支工作笔:https://codepen.io/chanceaclark/pen/JVpmMd