Upon receiving an object in my function containing the information below:
{
"name": "Grand modèle",
"description": "Par 10",
"price": 0,
"functional_id": "grand_modele_par_10",
"quantity": 2,
"amount": 0
}
I must scan the next array of objects to locate and remove it.
[
{
"name": "Matériel crémation",
"products": [
{
"file": "data:image/;base64,",
"name": "Sacs bordeaux",
"description": "Pour les crémations Référence",
"id": 12,
"path": "",
"items": [
{
"name": "Petit modèle",
"description": "Par 25",
"price": 0,
"functional_id": "petit_modele_par_25",
"quantity": 2,
"amount": 0
},
{
"name": "Grand modèle",
"description": "Par 10",
"price": 0,
"functional_id": "grand_modele_par_10",
"quantity": 2,
"amount": 0,
"itemAdded": false
}
]
}
]
},
{
"name": "Documents",
"products": [
{
"file": "data:image/;base64,",
"name": "Affiches procédure",
"description": "De prise en charge",
"id": 18,
"path": "",
"items": [
{
"price": 0,
"functional_id": "affiches_procedure",
"quantity": 1,
"amount": 0
}
]
}
]
}
]
I use a 'forEach' loop to find the item that matches the condition and remove it from the array of objects.
public deleteItem(item) {
this.fullCartInfo.forEach(category => {
category.products.forEach(product => {
product.items.forEach(itemAdded => {
if (item.functional_id === itemAdded.functional_id) {
this.fullCartInfo.splice(itemAdded);
}
});
});
});
this.cartService.removeItem(item);
}
However, instead of deleting only the matching item, the entire array gets emptied. What am I doing wrong here? Your help is much appreciated.