简体   繁体   English

如果 size 属性不同但 ID 键相同,则将商品添加到购物车

[英]Add item to cart if size property is different but the ID keys are the same

I created a cart push insert items into it, the logic is this:我在其中创建了一个购物车推送插入项目,逻辑是这样的:

I have an array:我有一个数组:

`let cartItems = [];`

To contain Product objects with defined properties:要包含具有已定义属性的 Product 对象:

{
"id": 10,
"name": "Nike Hoodie 1",
 ...
}

I would like to increase an item's quantity property and set size property (if the item exists in the array) any time I push a new item to the cartItems array.每当我将新项目推送到cartItems数组时,我想增加项目的quantity属性并设置大小属性(如果该项目存在于数组中)。 However, if the value of productSize of the new item is different from the existing item's productSize property, I would like to push this new item to the cartItems array with quantity: 1 rather than increasing the `quantity as I described earlier.但是,如果新商品的productSize值与现有商品的productSize属性不同,我想将这个新商品推送到quantity: 1cartItems数组中,而不是像我之前描述的那样增加 `quantity。

But the problem with id property appears because the id is the same for both products.但是出现了id属性的问题,因为两种产品的 id 相同。 And when I try to remove or update cartItems array, this would throw an error because I use the id property of the products to find and map the cartItems array.当我尝试删除或更新cartItems数组时,这会引发错误,因为我使用产品的id属性来查找和 map 的cartItems数组。 Please help?请帮忙?

I have tried to create a temporaryId property set to a random number for each product but I don't know how work this solution.我试图为每个产品创建一个设置为随机数的temporaryId属性,但我不知道这个解决方案是如何工作的。 This is the code I wrote:这是我写的代码:

export const addItemToCart = (cartItems, cartItemToAdd, selectedSize) => {
  const existingCartItem = cartItems.find(
    (cartItem) => cartItem.id === cartItemToAdd.id
  );
  if (existingCartItem) {
    if (existingCartItem.productSize === selectedSize) {
      console.log('product sizes are the same');
      return cartItems.map((cartItem) =>
        cartItem.id === cartItemToAdd.id
          ? {
              ...cartItem,
              quantity: cartItem.quantity + 1,
            }
          : cartItem
      );
    } else {
      console.log('product sizes are not the same');
      return cartItems.map((cartItem) =>
        cartItem.id === cartItemToAdd.id
          ? {
              ...cartItem,
              quantity: cartItem.quantity + 1,
              productSize: selectedSize,
            }
          : cartItem
      );
    }
  }
  let randomId = Math.floor(1000 + Math.random() * 9000);
  return [
    ...cartItems,
    {
      ...cartItemToAdd,
      tempId: randomId,
      quantity: 1,
      productSize: selectedSize,
    },
  ];
};

I think one way to do it is to modify your existingCartItem check.我认为一种方法是修改您existingCartItem的CartItem 检查。 Instead of finding the item with that id , find the index of the item that matches id AND productSize .不要查找具有该id的项目,而是查找与id AND productSize匹配的项目的索引。

 const existingCartItemIndex = cartItems.findIndex((cartItem) => {
     return cartItem.id === cartItemToAdd.id && cartItem.productSize === selectedSize;
  });
});

That way, if the index is > -1 you know the index of the item you need to modify, otherwise you know you need to add the item to the array.这样,如果索引 > -1,则您知道需要修改的项目的索引,否则您知道需要将项目添加到数组中。

So, a solution would look like this:因此,解决方案如下所示:

export const addItemToCart = (cartItems, cartItemToAdd, selectedSize) => {
  const existingCartItemIndex = cartItems.findIndex((cartItem) => {
     return cartItem.id === cartItemToAdd.id && cartItem.productSize === selectedSize;
  });

  if (existingCartItemIndex > -1) {
     cartItems[existingCartItemIndex].quantity = cartItems[existingCartItemIndex].quantity + 1;
  } else {
    cartItems.push({ 
      ...cartItemToAdd, 
      quantity: 1, 
      productSize: selectedSize
  }

  return cartItems;
};

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM