简体   繁体   中英

How to process Nested Array of json Objects with unknown key in handlebars

I have a query against database that returns more than one JSON Objects in response so creating valid JSON using -

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

console.log output / 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"
        }
    }]
}]

I am trying to print "re" value under "dec" object but it displays nothing, How to do it ?

    {{#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}}

if i put below code, it just prints object, so i think no validation issues in passing data -

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

displays -

[object Object]
[object Object]

This is how i am able to handle it for now, although not the best way -

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

in the handlebar -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM