I want to remove some element in my array using splice method with if condition in my iteration. I do the following:
var list = [{type:"product",name:"Product A"},{type:"product",name:"Product B"},{type:"service", name:"Service A"},{type:"service", name:"Service B"}]
list.forEach(function (item, index) {
if (item.type == 'service'){
list.splice(index, 1)
}
}
//result: list = list = [{type:"product",name:"Product A"},{type:"product",name:"Product B"},{type:"service", name:"Service A"}]
//expected: list = [{type:"product",name:"Product A"},{type:"product",name:"Product B"}]
I expect the both element with type "service" will be removed, but only the first element is removed.