dot.js 遍历对象

dot.js loop through object

使用 dot.js 模板引擎如何遍历对象?在下面的示例数据中,您如何遍历 "msg" 对象?

{
    "msg": {
        "1": {
            "a": "a1"
        },
        "2": {
            "b": "b2"
        }
    }
}

the website 上的示例来看,您似乎应该能够做到:

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

然而,您可能希望先使用 Javascript 稍微简化该对象,然后再将其传递给模板,以便于迭代。

最好的办法是先将 msg 转换为数组。以这种方式迭代要容易得多。之后,只需使用 jQuery 的 $.each().