I have an array similar than this one, called websiteproducts:
[{
"memberid": 280,
"state": "AL",
"product": "1",
"deleteProduct": 1,
"waitingDays": 0
},
{
"memberid": 280,
"state": "AL",
"product": "2",
"deleteProduct": 1,
"waitingDays": 0
},
{
"memberid": 280,
"state": "AL",
"product": "3",
"deleteProduct": 1,
"waitingDays": 0
},
{
"memberid": 280,
"state": "AL",
"product": "4",
"deleteProduct": 0,
"waitingDays": 0
}
]
And Im trying to delete all objects that have "deleteProduct" = 1 as follows:
for (let i = 0; i < websiteproducts.length; i++) {
if (websiteproducts[i].deleteProduct == 1) {
websiteproducts.splice(i, 1)
}
}
But only the first object is deleted.
I believe it has to do with the fact that Im deleting the item from the array on the fly.
But this one works, when copying the objects to a new array:
let finalProducts = []
for (let i = 0; i < websiteproducts.length; i++) {
if (websiteproducts[i].deleteProduct != 1) {
finalProducts.push(websiteproducts[i])
}
}
Is there a way to delete the objects straight from the array through a loop, without having to create a second array?
Thanks.