why does shift stop my loop over this array : Javascript
This is a discussion on why does shift stop my loop over this array within the Javascript forums in Programming Languages category; Hi, I have an array like the following: for(x=0;x<results.length;x++){ alert(results.length); extracted=results.shift(); alert(results.length); if(results.indexOf(extracted)== -1){ alert(extracted + "was 1") }else{ alert(extracted + "was more than 1"); alert("here" + results.toString()); alert(results.length); } } with a bunch of results in there. I loop over the results, starting at 8, outputting the number before the shift and the number after the shift, when I get to the number after the shift = 4 then the indexOf extracted in the array is obviously not -1 and I get the else, which tells me that the index of my searchstring was more than 1, and that ...
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| I have an array like the following: for(x=0;x<results.length;x++){ alert(results.length); extracted=results.shift(); alert(results.length); if(results.indexOf(extracted)== -1){ alert(extracted + "was 1") }else{ alert(extracted + "was more than 1"); alert("here" + results.toString()); alert(results.length); } } with a bunch of results in there. I loop over the results, starting at 8, outputting the number before the shift and the number after the shift, when I get to the number after the shift = 4 then the indexOf extracted in the array is obviously not -1 and I get the else, which tells me that the index of my searchstring was more than 1, and that the length of my results are 4. Then it stops looping. This happens when I get to the first instance of a value that is repeated in the array. If I do the following for(x=0;x<=results.length;x++){ alert(results.length); extracted=results.shift(); alert(results.length); alert(results.toString()); } then the same thing happens. the last results toString() I get is Sonia, me (2),Sonia, me (2),Sonia, me (2),Sonia, me (2) each Sonia, me (2) is an individual item. Thanks |
|
#2
| |||
| |||
| On Sep 10, 9:18 am, pantagruel <rasmussen.br...> wrote: > > If I do the following > > for(x=0;x<=results.length;x++){ > > alert(results.length); > extracted=results.shift(); > > alert(results.length); > alert(results.toString()); > > } > Commit this to memory as if it was from Gargantua: When writing an iterative loop on the members of an array, NEVER call a mutator on that array inside the loop or your code will behave like flies on a dung heap, erratic and buzzing all over the place. [had to stick in a scatalogical ref. :-)] You forgot to put in an alert for x. That will show you why it's not working the way you expect it to. Try this instead... while(results.length>0){ alert(results.length); extracted=results.shift(); alert(results.length); alert(results.toString()); } --- Geoff |
![]() |
| Thread Tools | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Method to stop the For loop in the middle of exection of For loop | usenet | labview | 0 | 12-17-2007 03:40 AM |
| stop while loop when for loop finished | usenet | labview | 4 | 08-09-2007 03:10 PM |
| circular shift array | usenet | C | 7 | 07-31-2007 04:33 AM |
| Re: While Loop and For Loop dont stop executing | usenet | labview | 0 | 07-30-2007 11:40 AM |
| Re: While Loop and For Loop dont stop executing | usenet | labview | 1 | 07-27-2007 02:10 PM |


