如何从对象数组中取出满足条件的对象?

how to remove the object that meets the condition from an array of objects?

我在我的函数中收到一个包含以下信息的对象

{
  "name": "Grand modèle",
  "description": "Par 10",
  "price": 0,
  "functional_id": "grand_modele_par_10",
  "quantity": 2,
  "amount": 0
}

我需要检查它所在的下一个对象数组以将其删除

[
  {
    "name": "Matériel crémation",
    "products": [
      {
        "file": "data:image/;base64,",
        "name": "Sacs bordeaux",
        "description": "Pour les crémations Référence",
        "id": 12,
        "path": "",
        "items": [
          {
            "name": "Petit modèle",
            "description": "Par 25",
            "price": 0,
            "functional_id": "petit_modele_par_25",
            "quantity": 2,
            "amount": 0
          },
          {
            "name": "Grand modèle",
            "description": "Par 10",
            "price": 0,
            "functional_id": "grand_modele_par_10",
            "quantity": 2,
            "amount": 0,
            "itemAdded": false
          }
        ]
      }
    ]
  },
  {
    "name": "Documents",
    "products": [
      {
        "file": "data:image/;base64,",
        "name": "Affiches procédure",
        "description": "De prise en charge",
        "id": 18,
        "path": "",
        "items": [
          {
            "price": 0,
            "functional_id": "affiches_procedure",
            "quantity": 1,
            "amount": 0
          }
        ]
      }
    ]
  }
]

为此,我遍历带有 'forEach' 的对象数组,找到满足相等条件的项目并将其删除

public deleteItem(item) {


        this.fullCartInfo.forEach(category => {
            category.products.forEach(product => {
                product.items.forEach(itemAdded => {
                    if (item.functional_id === itemAdded.functional_id) {
                        this.fullCartInfo.splice(itemAdded);
                    }
                });
            });
        });
        this.cartService.removeItem(item);
    }


我得到的是让他清空我的对象数组。怎样才能让他只删除符合条件的呢? 我的错误是什么? 谢谢大家的宝贵时间

Array.prototype.splice(start[, deleteCount[, item1[, item2[, ...]]]]) 期望应删除的第一个元素的索引 (start) 以及可选的应删除多少元素 (deleteCount)。

对于 start 索引,我们可以使用 Array.prototype.forEach() 回调的第二个参数,它是集合中当前元素的索引。

以下示例使用:

  • 一组精简的数据
  • 一个常规函数而不是您的 class 方法,只是因为它更容易使用。您可以将函数主体复制并粘贴到您的 deleteItem 方法中。

const objectToRemove = { "functional_id": "grand_modele_par_10" }

const objects = {
  fullCartInfo: [
    { "products": [{ "id": 12, "items": [{ "functional_id": "grand_modele_par_10" }] }] },
    { "products": [{ "id": 18, "items": [{ "functional_id": "affiches_procedure" }] }] }
  ]
}

function deleteItem(item) {
  this.fullCartInfo.forEach((category, index) => {  // the index we will use for .splice() when we've found a matching category
    category.products.forEach(product => {
      product.items.forEach(itemAdded => {
        if (item.functional_id === itemAdded.functional_id) {
          this.fullCartInfo.splice(index, 1);  // remove one item only
        }
      });
    });
  });
}

deleteItem.apply(objects, [objectToRemove]);
// .apply() is only used to set the value of <this> in deleteItem and therefor simulate its behavior as a class method.

console.log(objects);

但这只有在您只从集合中删除一项,或者如果您从末尾删除项目时才有效。否则 .fullCartInfo.forEach(...) 的索引将与您刚刚用 .splice() 修改的实际集合不同步。 例如。您删除索引 0 处的第一项,然后数组会将所有元素向左移动一个索引(索引 1 处的项目将位于索引 0,索引 2 处的项目将位于索引 1,.. .) 但回调中的索引不会更新。

如果您有可能删除多个项目,那么您应该使用 .filter() 或一个很好的旧 for 循环,它从最后一个项目迭代到第一个项目。

.filter()

const objectToRemove = { "functional_id": "grand_modele_par_10" }

const objects = {
  fullCartInfo: [
    { "products": [{ "id": 12, "items": [{ "functional_id": "grand_modele_par_10" }] }] },
    { "products": [{ "id": 18, "items": [{ "functional_id": "affiches_procedure" }] }] }
  ]
}

function deleteItem(item) {
  this.fullCartInfo = this.fullCartInfo.filter((category, index) => {
    let keepCategory = true;
    
    category.products.forEach(product => {
      product.items.forEach(itemAdded => {
        if (item.functional_id === itemAdded.functional_id) {
          keepCategory = false;
        }
      });
    });
    
    return keepCategory;
  });
}

deleteItem.apply(objects, [objectToRemove]);
console.log(objects);

for 反向循环

const objectToRemove = { "functional_id": "grand_modele_par_10" }

const objects = {
  fullCartInfo: [
    { "products": [{ "id": 12, "items": [{ "functional_id": "grand_modele_par_10" }] }] },
    { "products": [{ "id": 18, "items": [{ "functional_id": "affiches_procedure" }] }] }
  ]
}

function deleteItem(item) {
  for(let index = this.fullCartInfo.length - 1; index >= 0; index--) {
    this.fullCartInfo[index].products.forEach(product => {
      product.items.forEach(itemAdded => {
        if (item.functional_id === itemAdded.functional_id) {
          this.fullCartInfo.splice(index, 1);
        }
      });
    });
  }
}

deleteItem.apply(objects, [objectToRemove]);

console.log(objects);