node.js jade – 无法访问嵌套的数组元素

对不起,如果这是一个真正的基本问题,但我找不到任何类似于我想解决的问题的例子。

有人可以请解释为什么我不能访问下面的代码中的元素的嵌套数组,也是如何可以访问该数组中的元素? 从下面的json中,我无法得到从第二个结果find的“Items”数组。

下面的json正在返回:

{ "d": { "results": [ { "__metadata": { "uri": "...", "type": "..." }, "__index": 1, "ID": "someID1", "Name": "Some Name 1" }, { "__index": 2, "Items": [ { "__metadata": { "uri": "...", "type": "..." }, "ID": "itemID2_1", "Name": "Item 2_1" } ] }, { "__index": 3, "Items": [ { "__metadata": { "uri": "...", "type": "..." }, "ID": "itemID3_1", "Name": "Item 3_1" } ] }, ... 

这里是玉的布局:

 - var results=records, col_type='even'; table#results(style="border-collapse:collapse;") tr th.result-header Index th.result-header ID th.result-header Name - each r in results - col_type=col_type=='even' ? 'odd' : 'even' tr.result-row - if (!r.Items) td(class='result-col-'+col_type,style="border-left:1px solid black") #{r.__index} td(class='result-col-'+col_type,style="border-left:1px solid black") #{r.ID} td(class='result-col-'+col_type,style="border-left:1px solid black") #{r.Name} - else td(class='result-col-'+col_type,style="border-left:1px solid black") #{r.__index} - each i in r.Items td(class='result-col-'+col_type,style="border-left:1px solid black") #{i.ID} td(class='result-col-'+col_type,style="border-left:1px solid black") #{i.Name} 

这里的问题是你的JSON是这种格式

 { "d": { "results": [ ... ] 

所以你需要从你的玉石模板中改变这个部分

 - each r in results - col_type=col_type=='even' ? 'odd' : 'even' 

对此,

 - each r in results['d']['results'] - col_type=col_type=='even' ? 'odd' : 'even' 

这样,你的循环将通过每个数组项目。