I am developing an app with a shopping cart. When I add a product, I have to check if this product exist. I was using this method:
for (go over all products){
if(product exists){
--only change the quantity of the product[n]
}
else{
--create a new product object with all its properties
}
}
But with this method if the product I want to add don´t is the same with the index 0 (the first that the for checks), always add a new object (product) so if for example, my product exists and have the index 2, this for never will know that it exists.
Sorry for my english 😉 and thank you.
If I understood it right, you’re problem is, that you loop over all the products and if the product is not the same as the new one, you just add a new one.
but you’ll need to loop over all entries first and after you have done that and didn’t find anything, you create a new product.
So in pseudo code: