Javascript- Splitting an array I'm looping through without changing the loop array -
var arraylength = splitdata.length; for(var i=0; i<arraylength; i++){ if(splitdata[i]== '----------------\r\n#notes:\r\n'){ console.log("notes section found..."); notesection = true; } else if(i==1){ var temparray = splitdata[i]; var titledata = temparray; titlebox = titledata.split("\r\n"); for(var i=0; i<titlebox.length; i++){ var bpmbox = titlebox[i]; if(bpmbox.indexof("bpms") >= 0){ var bpmboxsplit = bpmbox.split("="); bpm = parseint(bpmboxsplit[1]); console.log("bpm found: " + bpm); } } }
so have array , loop iterates through array, searching particular string:
'----------------\r\n#notes:\r\n'
but reason, else if statement has code in that's altering arraylength, , causing loop skip straight on string need.
var titledata = splitdata[i]; titlebox = titledata.split("\r\n");
this bit of code messing things , don't understand why or how work around it. assume when titledata.split("\r\n") messing arraylength, since both referencing same array. i, again, don't understand why happen or know how prevent it.
basically, need work same array i'm looping through, without altering or changing original. figured making new variable enough accomplish this, guess not. because whatever 1 variable seems affect other.
any appreciated.
you have 2 nested loops, ok, can't use same variable both of them. in inner loop, redefining variable of outer loop, of course messes order of outer loop.
the solution simple: use different variable name in inner loop, example i2 oder ii.
Comments
Post a Comment