0
我想删除数组中的对象时,该对象的ID等于被比较的对象的ID。目前,它只能在数组中删除第一个对象角度2:删除数组中的对象
if(this.selectedProducts.length > 0){
for(let x of this.selectedProducts){
if(prod._id === x._id){
this.selectedProducts.splice(x,1); //this is the part where I 'delete' the object
this.appended = false;
}else{
this.appended = true;
}
}
if (this.appended) {
this.selectedProducts.push(prod);
}
}else{
this.selectedProducts.push(prod);
}
this.selectEvent.emit(this.selectedProducts);
}
可能是你的ID来作为一个或服用点。我没有看到与角度相关的任何事情 –
事件发射器是角度虽然@AniruddhaDas – Char
如果'selectedProducts'是一个字典,整个操作可能只是'selectedProducts [prod._id] = prod' – Pace