简体   繁体   English

从数组删除项目不起作用

[英]deleting item from array is not working

I have an array in the format below named cartarray. 我有一个名为cartarray的以下格式的数组。

1: {id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"}

2: {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "3"}

3: {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"}

the html below is populated by the array above 下面的html由上面的数组填充

cartarry.products.forEach(function(element) {
    var id = element.id;
    var itemname = element.name;
    var itemprice = element.price;
    var itemcard = `
              <div class="product" id="${id}">
                        <div class="product-image">
                          <img src="https://s.cdpn.io/3/dingo-dog-bones.jpg">
                        </div>
                        <div class="product-details">
                          <div class="product-title">Dingo Dog Bones</div>
                          <p class="product-description"> ${itemname}</p>
                        </div>
                        <div class="product-price">${itemprice}</div>
                        <div class="product-quantity">
                          <input type="number" value="2" min="1">
                        </div>
                        <div class="product-removal">
                          <button class="remove-product">
                            Remove
                          </button>
                        </div>
                <div class="product-line-price">25.98</div>
                </div>

            `;
    itemcontainer.innerHTML += itemcard;
})       

what I want to do is delete the clicked item from the array so I tried this 我想要做的是从数组中删除单击的项目,所以我尝试了

var items = cartarry.products;
$('.product').on('click', function(element) {

    console.log(this.id)

    var index = items.indexOf(this);
    console.log(index)

    var i = items.indexOf(this);
    if (i != -1) {
        items.splice(i, 1);
    }
})  

what I keep getting for index regardless of the item i clicked is -1 which keeps preventing the item from deleting. 无论我单击什么项目,我不断获得的索引都是-1,这可以防止该项目被删除。 How can I fix this and delete clicked item from the array? 如何解决此问题并从阵列中删除单击的项目?

First of all you created an "item" object from "cartarry.products", you will not be able to delete the element from cartarry.products because you are deleting it from item, you can use this code to find an element by an specific property: 首先,您从“ cartarry.products”创建了一个“ item”对象,由于要从item中删除该元素,因此您将无法从cartarry.products中删除该元素,可以使用此代码通过特定代码查找元素属性:

  var index = cartarry.products.map(function(element) {
          return element.id;
        }).indexOf(this.id);

I created a plunkr example for you: 我为您创建了一个plunkr示例:

https://embed.plnkr.co/qWlUy6NxAG3ERjDXD0LL/ https://embed.plnkr.co/qWlUy6NxAG3ERjDXD0LL/

You won't find this in your array, because this is a DOM element. 您不会在数组中找到this ,因为this是一个DOM元素。

You can search for the index of the item that contains your id instead, using Array.findIndex 您可以使用Array.findIndex来搜索包含您的ID的商品的索引

var findId = this.id;

var match = items.findIndex(function (el) {
  return el.id == findId;
});

if (match > -1) {
  items.splice(match, 1);
}
$('.product').on(
    'click',
    ({ id }) => cartarry.products = carrtarry.products.filter(pr => pr.id != id)
)

In your original code, this was the DOM element that was clicked, not your actual product. 在您的原始代码中, this是被单击的DOM元素,而不是您的实际产品。 The above code takes the id from the clicked element and takes only the cartarry products that do not have the clicked id . 上面的代码从clicked元素中获取id ,并仅获取没有clicked id的Cartarry产品。

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

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