【问题标题】:How to update cart array, when you have multiple objects in the cart according to conditions?当您根据条件在购物车中有多个对象时,如何更新购物车数组?
【发布时间】:2020-11-12 12:43:08
【问题描述】:

我是 JS 新手。我将产品详细信息作为对象。我需要在点击时更新我的​​购物车数组,

if (cart=== null)=> qty = 1 => push to cart array ;
else if ( same name && same lense ) => updating qty+1; (bcz there is a item that fulfill that condition) ;

else if ( same name && different  lense ) => qty = 1;=> then push to cart array;

else  ( different name && different lense  ) => qty = 1;=> then push to cart array;

但是当我推送(同名 && 同一个镜头)时,应该更新(同名 && 同一个镜头)的项目数量。但它仅更新索引 [0] 的数量。 我该如何解决这个问题?

这是我的购物车数组

let cart = JSON.parse(localStorage.getItem('productArray')) || [];``
> //new item object//
>//lense client has severel option//
const cartItem = {
    pic: singlePic.src,
    name: singleProductName.textContent,
    qty: 0,
    price: singleProductPrice,
    lense: "",
};
function addToCart() {
    //no item
    if (JSON.parse(localStorage.getItem('productArray')) == null) {
        //update the cart
        cartItem.qty = 1;
        //push to the cart
        cart.push(cartItem);
        localStorage.setItem('productArray', JSON.stringify(cart))
        // update the cart after the condition
        cart = JSON.parse(localStorage.getItem('productArray'))
    } else {
        // cart has some items
        for (let oldItem of cart) {
            if (cart.some(oldItem => oldItem.name === cartItem.name && oldItem.lense === cartItem.lense)) { oldItem.qty += 1
                // replace order with updated cart 
                localStorage.setItem("productArray", JSON.stringify(cart));
                cart = JSON.parse(localStorage.getItem("productArray"))
            }
            else if (cart.some(oldItem => oldItem.name === cartItem.name && oldItem.lense !== cartItem.lense)) {
                cartItem.qty = 1
                cart.push(cartItem)
                localStorage.setItem('productArray', JSON.stringify(cart))
                cart = JSON.parse(localStorage.getItem("productArray"))
            }
            else {
                cartItem.qty = 1
                cart.push(cartItem)
                localStorage.setItem('productArray', JSON.stringify(cart))
                cart = JSON.parse(localStorage.getItem("productArray"))

            }
        }
    }

【问题讨论】:

    标签: javascript local-storage e-commerce cart webshop


    【解决方案1】:

    在不太了解您的应用程序需求的情况下,这里有一个更新集合的基本实现。

    // Example cart array (typical data structure)
    let cart = [
        {
            id: 1,
            name: "Product 1",
            quantity: 1
        },
        {
            id: 2,
            name: "Product 2",
            quantity: 2
        }
    ]
    
    // Function to handle updates
    const update_cart = ( id, quantity, name ) => {
        let item = cart.find( item => item.id === id )
        // Product is already in cart, need to increment
        if ( item ) { 
            item.quantity = item.quantity + quantity
        }
        // Product is not in cart, need to add it
        else {
            cart.push({
                id,
                name,
                quantity
            })
        }
    }
    
    // Update a product in the cart
    update_cart(1, 2, "Product 1")
    console.log(cart)
    
    // Add a new product to the cart
    update_cart(3, 2, "Product 3")
    console.log(cart)
    

    这种类型的应用程序通常由 API 处理。但这肯定会为您指明正确的方向

    【讨论】:

      猜你喜欢
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多