2015-09-06 61 views
1

我想通過這個json對象循環,我想返回關聯的點,總點和百分點的'點'內的項目。返回數組的最後一項的循環

該代碼僅輸出> 100中的每個它遍歷

<% 
    var connection = appdata.cao.years, 
     year = null, 
     totalStudents = null, 
     points = null 

     for(var i=0; i < connection.length;i++) { 
      year = connection[i].year; 
      totalStudents = connection[i].total; 
       for(var j = 0; j < connection[i].points.length;j++){ 
       points = connection[i].points[j].id; 
      } 
    %> 
     <p> 
      <%=year %>, 
      <%=totalStudents %> 
      <%=points %> 
     </p> 

    <%}%> 

這裏是JSON

{ 
"name":"cao json", 
"description":"breakdown of cao statistics", 
"cao":{ 
    "id":1, 
    "years":[ 
     { 
      "id":"1", 
      "year":"2015", 
      "total":"55045", 
      "points":[ 
       { 
        "id":"2", 
        "points":"600", 
        "total":"215", 
        "percent":"0.4" 
       }, 
       { 
        "id":"3", 
        "points":"500-599", 
        "total":"5431", 
        "percent":"9.9" 
       }, 
       { 
        "id":"4", 
        "points":"400-499", 
        "total":"14097", 
        "percent":"25.6" 
       }, 
       { 
        "id":"5", 
        "points":"300-399", 
        "total":"14446", 
        "percent":"26.2" 
       }, 
       { 
        "id":"6", 
        "points":"200-299", 
        "total":"9768", 
        "percent":"17.7" 
       }, 
       { 
        "id":"7", 
        "points":"100-199", 
        "total":"6562", 
        "percent":"11.9" 
       }, 
       { 
        "id":"8", 
        "points":" >100", 
        "total":"4526", 
        "percent":"8.2" 
       } 
      ] 
     }, 
     { 
      "id":"9", 
      "year":"2014", 
      "total":"54025", 
      "points":[ 
       { 
        "id":"10", 
        "points":"600", 
        "total":"162", 
        "percent":"0.3" 
       }, 
       { 
        "id":"11", 
        "points":"500-599", 
        "total":"5088", 
        "percent":"9.4" 
       }, 
       { 
        "id":"12", 
        "points":"400-499", 
        "total":"13447", 
        "percent":"24.9" 
       }, 
       { 
        "id":"13", 
        "points":"300-399", 
        "total":"14047", 
        "percent":"26" 
       }, 
       { 
        "id":"14", 
        "points":"200-299", 
        "total":"9584", 
        "percent":"17.7" 
       }, 
       { 
        "id":"15", 
        "points":"100-199", 
        "total":"6926", 
        "percent":"12.8" 
       }, 
       { 
        "id":"16", 
        "points":" >100", 
        "total":"4771", 
        "percent":"8.8" 
       } 
      ] 
     }, 
     { 
      "id":"17", 
      "year":"2013", 
      "total":"52767", 
      "points":[ 
       { 
        "id":"18", 
        "points":"600", 
        "total":"152", 
        "percent":"0.3" 
       }, 
       { 
        "id":"19", 
        "points":"500-599", 
        "total":"4813", 
        "percent":"9.1" 
       }, 
       { 
        "id":"20", 
        "points":"400-499", 
        "total":"12803", 
        "percent":"24.3" 
       }, 
       { 
        "id":"21", 
        "points":"300-399", 
        "total":"13381", 
        "percent":"25.4" 
       }, 
       { 
        "id":"22", 
        "points":"200-299", 
        "total":"9566", 
        "percent":"18.1" 
       }, 
       { 
        "id":"23", 
        "points":"100-199", 
        "total":"6914", 
        "percent":"13.1" 
       }, 
       { 
        "id":"24", 
        "points":" >100", 
        "total":"5138", 
        "percent":"9.7" 
       } 
      ] 
     }, 
     { 
      "id":"25", 
      "year":"2012", 
      "total":"52589", 
      "points":[ 
       { 
        "id":"26", 
        "points":"600", 
        "total":"165", 
        "percent":"0.2" 
       }, 
       { 
        "id":"27", 
        "points":"500-599", 
        "total":"5026", 
        "percent":"9.6" 
       }, 
       { 
        "id":"28", 
        "points":"400-499", 
        "total":"12395", 
        "percent":"23.6" 
       }, 
       { 
        "id":"29", 
        "points":"300-399", 
        "total":"13170", 
        "percent":"25" 
       }, 
       { 
        "id":"30", 
        "points":"200-299", 
        "total":"9588", 
        "percent":"18.2" 
       }, 
       { 
        "id":"31", 
        "points":"100-199", 
        "total":"6999", 
        "percent":"13.3" 
       }, 
       { 
        "id":"32", 
        "points":" >100", 
        "total":"5276", 
        "percent":"10" 
       } 
      ] 
     }, 
     { 
      "id":"33", 
      "year":"2011", 
      "total":"54341", 
      "points":[ 
       { 
        "id":"34", 
        "points":"600", 
        "total":"162", 
        "percent":"0.3" 
       }, 
       { 
        "id":"35", 
        "points":"500-599", 
        "total":"4863", 
        "percent":"8.6" 
       }, 
       { 
        "id":"36", 
        "points":"400-499", 
        "total":"12235", 
        "percent":"22.5" 
       }, 
       { 
        "id":"37", 
        "points":"300-399", 
        "total":"13860", 
        "percent":"18.4" 
       }, 
       { 
        "id":"38", 
        "points":"200-299", 
        "total":"9966", 
        "percent":"18.4" 
       }, 
       { 
        "id":"39", 
        "points":"100-199", 
        "total":"7477", 
        "percent":"13.8" 
       }, 
       { 
        "id":"40", 
        "points":" >100", 
        "total":"5928", 
        "percent":"10.9" 
       } 
      ] 
     }, 
     { 
      "id":"34", 
      "year":"2010", 
      "total":"54480", 
      "points":[ 
       { 
        "id":"35", 
        "points":"600", 
        "total":"136", 
        "percent":"0.2" 
       }, 
       { 
        "id":"36", 
        "points":"500-599", 
        "total":"4564", 
        "percent":"8.4" 
       }, 
       { 
        "id":"37", 
        "points":"400-499", 
        "total":"11973", 
        "percent":"22" 
       }, 
       { 
        "id":"38", 
        "points":"300-399", 
        "total":"13878", 
        "percent":"25.5" 
       }, 
       { 
        "id":"39", 
        "points":"200-299", 
        "total":"10391", 
        "percent":"19.1" 
       }, 
       { 
        "id":"40", 
        "points":"100-199", 
        "total":"7294", 
        "percent":"13.4" 
       }, 
       { 
        "id":"41", 
        "points":" >100", 
        "total":"6244", 
        "percent":"11.5" 
       } 
      ] 
     } 
    ] 
} 

}

的代碼的JavaScript,包裹時間,該時間的最後一個項目在expressJS標籤內

+1

你使用什麼模板'<%= %>'?這不是JavaScript或JSON。 –

+0

請添加更多描述。錯誤是什麼,你試圖達到什麼目標等等。此外,完整的代碼示例不是必需的,它使帖子難以閱讀。 –

+0

@DavinTryon我認爲這是ASP或JSP?不確定:/ –

回答

0

你有可變points = null是ressign每一個循環迭代,所以不是

points = connection[i].points[j].id; 

你應該使用

points.push(connection[i].points[j].id); 

,改變之前

points = null 

points = [] 
+0

完美運作 –

0

您的循環應延伸到<p>標籤,以便每個項目都會打印到頁面上。到目前爲止,您的整個循環將在輸出階段之前執行,並且所有先前的值在過程中被覆蓋。所以在輸出塊之後移動右括號。

相關問題