开发者

For loop inside For Loop Javascript

For some reason this statement is skipping some data.Am I missing a continue statement somewhere or something ? Here is the code

for (var i = 0, len = data.ORDER_STATUS[0].ORDERS.length; i < len; i++) {
  if (data.ORDER_STATUS[0].ORDERS[i].SEC_TYPE == "MLEG") {
    for (var i = 0; i < data.ORDER_STATUS[0].OR开发者_运维百科DERS[i].LEGS.length; i++) {
      LEGS += '<tr class="MLEGS"><td class="orderFirst">' +
        data.ORDER_STATUS[0].ORDERS[i].LEGS[i].SYMBOL +
        '</td><td>' + data.ORDER_STATUS[0].ORDERS[i].LEGS[i].ACTION +
        '</td><td>' + data.ORDER_STATUS[0].ORDERS[i].LEGS[i].QTY +
        '</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
    }
  }
}


Use a different variable on the inner loop, like j instead of i.

for (var i = 0, len=data.ORDER_STATUS[0].ORDERS.length; i < len; i++) {
    //...

   for (var j = 0; j < data.ORDER_STATUS[0].ORDERS[i].LEGS.length; j++){
       //...
       data.ORDER_STATUS[0].ORDERS[i].LEGS[j].SYMBOL + 


you are using "i" in your outer an inner loops. you need to use a different variable in the inner loop: i have used "inner" below as and example.

for (var i = 0, len=data.ORDER_STATUS[0].ORDERS.length; i < len; i++) {
    if (data.ORDER_STATUS[0].ORDERS[i].SEC_TYPE=="MLEG"){
      for (var inner = 0; inner  < data.ORDER_STATUS[0].ORDERS[i].LEGS.length; inner ++) {
          // do something
      }
    }
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜