9

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].ORDERS[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>';
    }
  }
}
2
  • 3
    Why in the world is it formatted like that? It looks like it's generated from a template file or something. Commented Aug 15, 2011 at 23:07
  • should you be using the i variable both times? Commented Aug 15, 2011 at 23:09

2 Answers 2

17

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 + 
Sign up to request clarification or add additional context in comments.

7 Comments

That definitely works, but now the inner loop is only returning the first array key in that object when there are 4. hmmm
@Bootleg: I would need to see your data to know why. Could you post it in the question?
@Bootleg: Any errors in the console? Hard to tell from an image what's going on. Could you posted your updated code in your question? Don't erase the original code. Just put the new code at the bottom.
@Bootleg: Ah those tricky CSS issues! Not a problem. Glad you got it figured out.
@Greg: Thank you for pointing that out. I forgot to update that variable. But let me tell you something. You don't need to down-vote every time someone makes a minor mistake. I help to correct people's answers all the time without down-voting. I corrected yours the other day without down-voting you (or the others who had incorrect answers). In fact it's rare that I vote anyone down. If you look at my profile, you'll see that of my 3,434 votes, 96 are down. I'd rather help someone...
|
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.