Skip to content
Advertisement

Update quantity of items in cart without pushing entirety of object in JS

I’m having a bit of trouble with this problem. I’m working on the project of an e-commerce application that works on several html pages. I managed to push products through the cart html page, but I can’t seem to find a way to update on this page only the quantity of a product and not push every elements of said product (images, id, etc). Onclick, if product exists, I only want quantity to be updated. Here’s the code if any of you can help me out that’d be greatly appreciated.

JavaScript
JavaScript

Advertisement

Answer

There’s a few approaches you can take, but I am using .find to look through your cart.

If the .find() function finds an item with the same id as you’re about to add, it will up the quantity of the existing item instead of appending another object with the same ID.

I used a mock local storage since local storage doesn’t work in these snippets so just ignore that and use what you’ve been doing for local storage access.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement