最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to loop through a shrinking array - Stack Overflow

programmeradmin1浏览0评论

Suppose I have an array like this:

myArray = ["a","b","c","d","e"]

And I want to loop through it to find specific values and remove them.

for(var i=0;i<myArray.length;i++){
    if(myArray[i] == "b")
        myArray.splice(i,1)
}

the problem being, splice removes the item from the array, and all the items in front of the removed one, shift down an index number, so myArray.length was instantiated as 5 but after the splice myArray has a length of only 4 and the for loop fails since myArray[4] throws typeof match error in the framework.

I'm using a framework that works this way, that's why I'm utilizing such an item removal technique, my question is how to go about doing this the right way? The framework uses the splice method, I'm using the for loop, so I assume there's a correct way to go about this?

Suppose I have an array like this:

myArray = ["a","b","c","d","e"]

And I want to loop through it to find specific values and remove them.

for(var i=0;i<myArray.length;i++){
    if(myArray[i] == "b")
        myArray.splice(i,1)
}

the problem being, splice removes the item from the array, and all the items in front of the removed one, shift down an index number, so myArray.length was instantiated as 5 but after the splice myArray has a length of only 4 and the for loop fails since myArray[4] throws typeof match error in the framework.

I'm using a framework that works this way, that's why I'm utilizing such an item removal technique, my question is how to go about doing this the right way? The framework uses the splice method, I'm using the for loop, so I assume there's a correct way to go about this?

Share Improve this question edited Oct 22, 2015 at 15:12 RenaissanceProgrammer asked Oct 22, 2015 at 15:01 RenaissanceProgrammerRenaissanceProgrammer 4841 gold badge12 silver badges30 bronze badges 5
  • i not quite understand, what you mean here: myArray[4] throws undefined error – Grundy Commented Oct 22, 2015 at 15:04
  • Save yourself the trouble and use` Array.map` or create a new array. – Saravana Commented Oct 22, 2015 at 15:05
  • 1 @Saravana, in this case better filter :-) – Grundy Commented Oct 22, 2015 at 15:06
  • @Grundy sorry about that, it's an error thrown in the framework because it's expecting a typeof to match, it's not an undefined error, i'll remove that bit – RenaissanceProgrammer Commented Oct 22, 2015 at 15:09
  • Just replace from myArray.splice(i,1); to myArray.splice(i++,1);. Then your code will work – Sherali Turdiyev Commented Oct 22, 2015 at 15:57
Add a ment  | 

5 Answers 5

Reset to default 6

Reverse the loop:

for(var i=myArray.length-1;i>=0;i--){
    if(myArray[i] == "b")
        myArray.splice(i,1)
}

Consider using filter() to determine which elements you want. For instance:

var myArray = ["a","b","c","d","e"];
var myNewArray = myArray.filter(function(elem) { return elem !== "b"; });

Use the filter function:

var myArray = ["a","b","c","d","e"];
myArray.filter(function(item) {
    return item !== "b";
});

If you must use splice, then you could increment i in the loop and simply not increment it if the item is removed:

for(var i = 0; i < myArray.length;) {
  if(myArray[i] == "b") {
    myArray.splice(i, 1);
  } else {
    ++i;
  }
}

Just replace from i to i++ in yourmyArray.splice(i,1)

for(var i = 0; myArray[i]; i++){
    if(myArray[i] == "b")
        myArray.splice(i++,1)
}
发布评论

评论列表(0)

  1. 暂无评论