【问题标题】:How to process Nested Array of json Objects with unknown key in handlebars如何在车把中处理带有未知键的 json 对象的嵌套数组
【发布时间】:2019-08-02 12:03:56
【问题描述】:

我有一个针对数据库的查询,它返回多个 JSON 对象作为响应,因此使用 -

创建有效 JSON
 var intObj = JSON.parse("[" + response + "]");
 console.log(JSON.stringify(intObj));
 resp.render('abc', {details : intObj});

console.log 输出 / intObj -

    [{
    "0c057c2b-a4fa-4b8a-89b9-abebf2006704": [{
        "tah": {
            "id": "92b40e40-58d2-48ff-bfbc-9318b9637ffb",
            "ldt": 1564715961014
        }
    }, {
        "th": {
            "ldt": 1564715952873
        }
    }, {
        "aah": {
            "id": "eb90c2d1-95a1-4f4c-97c3-cbc55a95c57a",
            "ldt": 1564715961019
        }
    }, {
        "ah": {
            "ldt": 1564715952885
        }
    }, {
        "dec": {
            "re": "abc"
        }
    }]
    }, 
    {
    "7589668e-62b4-4339-ac06-7591ea1490d3": [{
    .....
        "dec": {
            "re": "test1"
        }
    }]
}]

我试图在“dec”对象下打印“re”值,但它什么也没显示,怎么办?

    {{#each details}}
      {{#each ../dec}}     // tried  {{#each this}} and {{#each dec} and putting another {{#each this}} between details and dec no change in output
        <div class="row">{{re}}</div>
      {{/each}}
    {{/each}}

如果我把代码放在下面,它只是打印对象,所以我认为在传递数据时没有验证问题 -

{{#each details}}
    <div class="row">{{this}}</div>
{{/each}}

显示 -

[object Object]
[object Object]

【问题讨论】:

    标签: node.js json handlebars.js render


    【解决方案1】:

    这就是我现在能够处理的方式,虽然不是最好的方式 -

    const expresshandlebars = require('express-handlebars');
    app.engine('handlebars', expresshandlebars({helpers: { json: function (context) { return JSON.stringify(context); } } }));
    

    在车把上 -

    {{#each details}}
      {{#each this}}
        <div class="row">{{json this.[4].dec.re}}</div>
      {{/each}}
    {{/each}}
    

    【讨论】:

      猜你喜欢
      • 2012-06-17
      • 1970-01-01
      • 2022-01-24
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      相关资源
      最近更新 更多