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?
-
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);
tomyArray.splice(i++,1);
. Then your code will work – Sherali Turdiyev Commented Oct 22, 2015 at 15:57
5 Answers
Reset to default 6Reverse 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)
}