Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Why second FOR loop don't work ?

toget = new Array("var18", "var4", "var43");

for (var i=0; i < toget.length; i++) {

    for (var x=0; x < result.toget[i].list.length; x++) {
    alert(x);
    }

}

If I do like this:

for (var i=0; i < toget.length; i++) {

    for (var x=0; x < result.var18.list.length; x++) {
    alert(x);
    }

}

It works but this is not correct way.

I need that values in toget array become variables to process them in second FOR loop.

Any ideas ? Thanks.

share|improve this question
    
can you clarify what "result" is? –  shoebox639 Aug 8 '11 at 20:16
1  
"result.var18.list.length" is what? –  Diodeus Aug 8 '11 at 20:16
    
Your code is missing what result is. Only guessing is possible for an answer until you include all relevant code. –  jfriend00 Aug 8 '11 at 20:18
2  
what is "result"? and better yet, why is this tagged as jquery? –  Sinetheta Aug 8 '11 at 20:18
    
... where and how is jQuery coming into play here? –  vector Aug 8 '11 at 20:19

2 Answers 2

up vote 3 down vote accepted
for (var i = 0; i < toget.length; i++) {
    for (var x = 0; x < result[toget[i]].list.length; x++) {
        alert(x);
    }
}
share|improve this answer

Try jquery each loop even better than for loop

toget = new Array("var18", "var4", "var43");

$.each(toget, function(i){
    $.each(result[toget[i]].list, function(x){
      alert(x);
    });
});
share|improve this answer
    
result.toget[i].list is incorrect here. It should be result[toget[i]].list. –  Rocket Hazmat Aug 8 '11 at 20:25
    
@Rocket - Missed it, anyways I fixed it thanks. –  ShankarSangoli Aug 8 '11 at 20:27
    
I didn't know about the $.each() function. It looks very useful to me. –  Jon Trauntvein Aug 8 '11 at 21:53
    
Yes, it is very useful take a look at the documentation here –  ShankarSangoli Aug 8 '11 at 21:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.