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

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -