简体   繁体   English

我如何从 object 内部删除项目

[英]how do i remove item from inside object

I have an array of objects like我有一个对象数组,比如

 let = cartItem =
   {
    "_id": "6075a0ddf88a6035001376d0",
    "buyerId": "603e808bab11752d38c463cb",
    "buyerEmail": "hassaanather00@gmail.com",
    "buyerName": "Muhammad  Hassaan Ather",
    "products": [
          {
              "_id": "6075a9afb5335c2e94b29c1b",
              "productId": "606ddc2a4b7adbd74c703746",
              "quantity": 4,
              "productTitle": "New",
              "productSelectedFile": "URL",
              "productPrice": 10000
          },
          {
              "_id": "6075abf8b5335c2e94b29c1c",
              "productId": "605df90844bf6e0264544b77",
              "quantity": 1,
              "productTitle": "Wooden",
              "productSelectedFile":             
              "anyUrl",
              "productPrice": 100000
          }
        ],

      }

Now i want to remove one of the PRODUCT in the cartItem object.现在我想删除cartItem object 中的一个产品 I have tried this method but after updating it also make a mess to the original CARTITEM .我已经尝试过这种方法,但在更新后它也会对原来的CARTITEM 造成混乱。

  let newCart = cartItems.products.filter((item) => item.productId !== id);

In the end the original cartItem object is also not showing anything.最后,原始 cartItem object 也没有显示任何内容。

Waht i want is to remove one product from products array and send back the remaining array.我想要的是从产品阵列中删除一个产品并将剩余的阵列发回。

You are not too far off.你离得太远了。 Your initial solution only returns the new array products and not your whole object.您的初始解决方案仅返回新的阵列产品,而不是您的整个 object。 You just need to add another line of code to add the new array of products into a new object of cartItems.您只需添加另一行代码即可将新的产品数组添加到新的购物车项目 object 中。

let newCartItemProducts = cartItems.products.filter((item) => item.productId !== id);
let newCartItems = {...cartItems, products: newCartItemProducts}

 let = cartItem = { "_id": "6075a0ddf88a6035001376d0", "buyerId": "603e808bab11752d38c463cb", "buyerEmail": "hassaanather00@gmail.com", "buyerName": "Muhammad Hassaan Ather", "products": [ { "_id": "6075a9afb5335c2e94b29c1b", "productId": "606ddc2a4b7adbd74c703746", "quantity": 4, "productTitle": "New", "productSelectedFile": "URL", "productPrice": 10000 }, { "_id": "6075abf8b5335c2e94b29c1c", "productId": "605df90844bf6e0264544b77", "quantity": 1, "productTitle": "Wooden", "productSelectedFile": "anyUrl", "productPrice": 100000 } ] } //delete 6075abf8b5335c2e94b29c1c for (var i = 0; i < cartItem["products"].length; i++) { if (cartItem["products"][i]["_id"] == "6075abf8b5335c2e94b29c1c") { cartItem["products"].splice(i, 1); break; } } console.log(cartItem);

Try this:尝试这个:

removeProduct(id){
   let index = cartItem.products.findIndex((product: any) => product._id === id);
   if (index != -1) cartItem.products.splice(index,1);
   return cartItem.products;
}

It will remove a product with specified id from the products of the cartItem and it will return the remaining products .它将从cartItemproducts中删除具有指定id的产品,并将返回剩余的products

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

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