javascript - How to loop through a shrinking array -
suppose have array this:
myarray = ["a","b","c","d","e"]
and want loop through find specific values , remove them.
for(var i=0;i<myarray.length;i++){ if(myarray[i] == "b") myarray.splice(i,1) }
the problem being, splice removes item array, , items in front of removed one, shift down index number, myarray.length
instantiated 5 after splice
myarray
has length of 4 , for
loop fails since myarray[4]
throws typeof match error in framework.
i'm using framework works way, that's why i'm utilizing such item removal technique, question how go doing right way? framework uses splice
method, i'm using for
loop, assume there's correct way go this?
reverse loop:
for(var i=myarray.length-1;i>=0;i--){ if(myarray[i] == "b") myarray.splice(i,1) }
Comments
Post a Comment