1

I want to add the addItem to the cartItems list, but if the id is the same as the item that already in the cartItems, I don't want to add another object, instead it will add the previous amountPrice and orderAmount with the new one.

Is there a way for me to achieve that?

const [cartItems, setCartItems] = useState(cartLocalStorage());

const addToCart = (id, name, image, amountPrice, orderAmount) => {
    const addItem = {
      id: id,
      name: name,
      image: image,
      amountPrice: amountPrice,
      orderAmount: orderAmount,
    };
    setCartItems([...cartItems, addItem]);
  };
};
2
  • This part is a little unclear instead it will add the previous amountPrice and orderAmount with the new one. Can you elaborate? Commented Mar 4, 2021 at 6:31
  • so before the new data got stored in the array, first it will check if the id is already there, which means it won't create another object, but change only the other 2 key properties, amountPrice and orderAmount. And it won't overwrite it, but it will add the new value which the sum of the old value + new value Commented Mar 4, 2021 at 7:35

2 Answers 2

1

Give this a shot:

const addToCart = (id, name, image, amountPrice, orderAmount) => {
    if(cartItems.find(i => i.id === id)){
        setCartItems(cartItems.map(item => {
            if(item.id === id){
                item.amountPrice += amountPrice;
                item.orderAmount += orderAmount;
            }
            return item;
        }));
    }else{
        setCartItems([...cartItems, {id, name, image, amountPrice, orderAmount}]);    
    }
}

addToCart will check if the item is already in cartItems by id and if so, will add the values of amountPrice and orderAmount to the corresponding values of the existing element.

If, however, the id is not in cartItems, it will add a new element to the array.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks it works, but I have a question. Why did you use find() not filter() on this case?
@BaBi I think using find in this context is a bit more intuitive. I could certainly use filter and the result would be the same, but filter tends to be usually used in situations where you need multiple items of an array returned. Since I know there is only one that can match the id, find appeared more appropriate for readability.
oh now I understand these concepts, thanks I learned a lot from you.
0

I have not tested it and will probably not work but hopefully will set you in the right direction:

const [cartItems, setCartItems] = useState(cartLocalStorage());

const addToCart = (id, name, image, amountPrice, orderAmount) => {
    const addItem = {
        id: id,
        name: name,
        image: image,
        amountPrice: amountPrice,
        orderAmount: orderAmount,
    };
    const index = cartItems.findIndex(item => item.id === id);
    if (index !== -1) {
        cartItems[index] = {...cartItems[index], ...addItem};
        setCartItems(cartItems);
    } else {
        setCartItems([...cartItems, addItem]);
    }
};

Cheers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.