Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dot.js loop through object

With dot.js template engine how do you loop through an object? In the example data below how do you loop through the "msg" object?

{
    "msg": {
        "1": {
            "a": "a1"
        },
        "2": {
            "b": "b2"
        }
    }
}
like image 705
user3658423 Avatar asked May 03 '15 07:05

user3658423


1 Answers

From the example on the website, it looks as if you should be able to do:

{{ for(var prop in it) { }}
<div>{{=prop}}</div> <!-- Prints "msg" -->
    {{ for(var msgProp in it[prop]) { }}
    <div>{{=msgProp}}</div> <!-- Prints "1" and "2" -->
        {{ for(var numProp in it[prop][msgProp]) { }}
        <!-- Prints "a: a1" and "b: b1" -->
        <div>{{=prop}}: {{=it[prop][msgProp][numProp]}}</div>
        {{ } }}
    {{ } }}
{{ } }}

However you may want to simplifiy that object a little bit with Javascript first, before passing it to the template in order to make it easier to iterate.

like image 107
CodingIntrigue Avatar answered Sep 18 '22 00:09

CodingIntrigue