I'm trying to delete all matched items from an array but it leaves Always one item in it.
var item1 = {item: "item1"},
array = [{
item: "item1"},{
item: "item_non"},{
item: "item1"},{
item: "item_non"},{
item: "item1"},{
item: "item1"},{
item: "item1"},{
item: "item_non"},{
item: "item_non"
}];
array.forEach(function(items){
if(item1.item === items.item){
var index = array.indexOf(items);
if(index !== -1){
array.splice(index,1);
}
}
});
I also fiddle it, it deletes only 4/5 items that matches instead of 5/5.
There is no option to use Array#filter I need to delete the objects.
.forEach()is the fundamental problem. The right way to do this is with.filter()or with a simpleforloop. If you think you can't use.filter(), you should explain why.