4
rated 0 times
[
9]
[
5]
/ answers: 1 / hits: 24522
/ 11 Years ago, sun, december 22, 2013, 12:00:00
I want to remove an element in an array with multiple occurrences with a function.
var array=[hello,hello,world,1,world];
function removeItem(item){
for(i in array){
if(array[i]==item) array.splice(i,1);
}
}
removeItem(world);
//Return hello,hello,1
removeItem(hello);
//Return hello,world,1,world
This loop doesn't remove the element when it repeats twice in sequence, only removes one of them.
Why?
More From » arrays