javascript - JS: For loop continues to iterate after condition is false -


does know cause loop continue after conditions met? post entire file, quite large. here loops:

for (var j = 0; j < depkeyarr.length; j++) {     var directory = angular.copy(dependencies[depkeyarr[j]]),         keyarr = angular.copy(object.keys(directory));     (var = 0; < keyarr.length; i++) {         var next = + 1;                 if (keyarr[i] && !keyarr[i].includes('params')) {             if (keyarr[next] && keyarr[next].includes('params')) {                 directory[keyarr[next]] = directory[keyarr[next]].split(', ') !== [''] ? directory[keyarr[next]].split(', ') : undefined                 directory[keyarr[next]].push(directory[keyarr[i]])             }             var paramarr = directory[keyarr[next]] ? directory[keyarr[next]] : directory[keyarr[i]]             switch (depkeyarr[j]) {                 case 'controller':                     angular.module('app').controller(keyarr[i], paramarr)                     break                 case 'directive':                     angular.module('app').directive(keyarr[i], paramarr)                     break                 case 'factory':                     angular.module('app').factory(keyarr[i], paramarr)                     break                 case 'filter':                     angular.module('app').filter(keyarr[i], paramarr)                     break                 case 'service':                     angular.module('app').service(keyarr[i], paramarr)                     break              }         }     } } 

so depkeyarr.length in particular instance equals 5, , keyarr.length equals 42. both loops continue execute when conditions met, in spite of fact expect them stop @ 4 , 41. can offer can figure out why great.

snippet updated above!

also - mentioned & j changing, when i'm looking @ in debugger, unchanged. looking @ right - j = 5 , depkeyarr.length 5. similarly, = 42 , keyarr.length 42...and yet i'm inside both loops, @ last case in switch statement. fails when keyarr[i] undefined.

in response answer provided obdm, added angular.copy() prevent updating array inside loop. still stepping loop when boolean condition met. more perplexing fact if condition doesn't keep executing switch. should fail enter following if:

if (!keyarr[i].includes('params')) 

because keyarr[i] undefined.

latest update -

it seems have broken out of inner loop latest changes, still continues switch statement , breaks on keyarr[i] though switch within both loops , if conditional evaluates false.

genrally speaking bad practice set stop condition of loop "dynamic value" i.e value can changed while iterating on loop.

the following code generates infinite loop:

for (var j = 0; j < depkeyarr.length; j++) {    depkeyarr.push("whatever"); } 

rather setting stop condition in variable prevents unclear behavior:

var depkeyarrlen = depkeyarr.length;  (var j = 0; j < depkeyarrlen; j++) {    depkeyarr.push("whatever"); } 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -