2011-06-04 7 views
5

क्यों रास्ते में नेस्टेड for loops काम है कि वे निम्न उदाहरण में करते हैं:जावास्क्रिप्ट: लूप काम कैसे नेस्टेड के लिए के बारे में उलझन

var times = [ 
      ["04/11/10", "86kg"], 
      ["05/12/11", "90kg"], 
      ["06/12/11", "89kg"] 
]; 

for (var i = 0; i < times.length; i++) { 
     var newTimes = []; 
     for(var x = 0; x < times[i].length; x++) { 
      newTimes.push(times[i][x]); 
      console.log(newTimes); 


     } 

    } 

इस उदाहरण में मैंने सोचा था कि console.log होता मुझे देना होगा निम्नलिखित उत्पादन:

["04/11/10"] 
["86kg"] 
["05/12/11"] 
["90kg"] 
["06/12/11"] 
["89kg"] 

हालांकि, मैं वास्तव में इस मिल:

["04/11/10"] 
["04/11/10", "86kg"] 
["05/12/11"] 
["05/12/11", "90kg"] 
["06/12/11"] 
["06/12/11", "89kg"] 

क्या कोई इसे समझने में मेरी सहायता कर सकता है?

संपादित करें:

आपके सभी प्रतिक्रियाओं के लिए धन्यवाद!

उत्तर

9

आप प्रत्येक एकल लूप पर newTimes को फिर से परिभाषित कर रहे हैं और आप प्रत्येक कॉलम पुश पर कंसोल पर आउटपुट कर रहे हैं।

var times = [ 
      ["04/11/10", "86kg"], 
      ["05/12/11", "90kg"], 
      ["06/12/11", "89kg"] 
]; 
var newTimes = []; 
for (var i = 0; i < times.length; i++) {  
     for(var x = 0; x < times[i].length; x++) { 
      newTimes.push(times[i][x]); 
     } 
    } 
    console.log(newTimes); 

रिटर्न: ["04/11/10", "86kg", "05/12/11", "90kg", "06/12/11", "89kg"] http://jsfiddle.net/niklasvh/SuEdt/

0

यह करें:

var newTimes = []; 
for (var i = 0; i < times.length; i++) { 
     for(var x = 0; x < times[i].length; x++) { 
      newTimes.push(times[i][x]); 
      console.log(newTimes); 


     } 

    } 

आप फिर से आरंभ कर रहे हैं NewTimes लूप के माध्यम से हर बार।

1

आप उत्पादन अगर उचित लॉग बयान

console.log(times[i][x]); 

पढ़ा होगा इसके बजाय आप अपना पूरा नयी सूची newTimes जो भीतरी लूप के बाहर प्रारंभ और प्रत्येक भीतरी पाश यात्रा के साथ बढ़ता है उत्पादन।

1

समस्या आंतरिक पाश के दूसरे दौर में है, जहां यह दूसरे तत्व को नए समय में धक्का देती है। वैसे भी मैं आंतरिक पाश के कारण को समझ नहीं पा रहा हूं। आप बहुत सरल लिख सकते हैं:

var times = [ 
      ["04/11/10", "86kg"], 
      ["05/12/11", "90kg"], 
      ["06/12/11", "89kg"] 
]; 

for (var i = 0; i < times.length; i++) { 
    console.log(time[i][0]); 
    console.log(time[i][1]); 
} 
3
// remember that the increment of the counter variable 
// is always executed after each run of a loop 


for (var i = 0; i < n; i++) { 
    // some statement(s) to do something.. 

     // initializes child-loop counter in the first run of the parent-loop 
     // resets child-loop counter in all following runs of the parent-loop 
     // while i is greater than 0 and lower than n 

    for (var j = 0; j < p; j++) { 
     // some statement(s) to do something.. 

      // initializes grandchild-loop counter in the first run of the child-loop 
      // resets grandchild-loop counter in all following runs of the child-loop 
      // while j is greater than 0 and lower than p 

     for (var k = 0; k < q; k++) { 
      // some statement(s) to do something.. 
      // or add more internal loop-nestings if you like.. 
     } 
    } 
} 

// if the counter variables of the descendent-loops were set before the loop-nesting, 
// the inner loops would only run once, because the counter would keep the value 
// of the abortion condition after the loop is finished 
संबंधित मुद्दे