在模板中渲染嵌套键

问题描述 投票:0回答:1

我有一个分组的数组,我想通过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)

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

javascript node.js pug
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.