Javascript 如何检查推入对象的对象数组是否具有来自另一个对象数组的特定键值

Javascript 如何检查推入对象的对象数组是否具有来自另一个对象数组的特定键值,javascript,arrays,angular,typescript,object,Javascript,Arrays,Angular,Typescript,Object,这就是我到目前为止所做的,以及我已经尝试过的: groceryList = []; ngOnInit() { this._recipesSub = this.recipesService.recipes.subscribe((receivedData) => { this.loadedRecipes = receivedData.recipes; }); } onCheckRecipe(e) { if (e.detail.checked === true) {

这就是我到目前为止所做的,以及我已经尝试过的:

groceryList = [];

ngOnInit() {
  this._recipesSub = this.recipesService.recipes.subscribe((receivedData) => {
    this.loadedRecipes = receivedData.recipes;
  });
}
onCheckRecipe(e) {
    if (e.detail.checked === true) {
      let tickedRecipe = e.detail.value;
      let selectedRecipeIngredients;
      for (let recipe of this.loadedRecipes) {
        if (recipe.name === tickedRecipe) {
          selectedRecipeIngredients = recipe.ingredients;
        }
      }
      for (let selectedIng of selectedRecipeIngredients) {
        for (let existingIng of this.groceryList) {
          if (selectedIng.name) {
            this.groceryList.push(selectedIng);
          }
        }
      }
      console.log(this.groceryList);
    }
  }
这是我在控制台中收到的信息:

(8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {_id: "5f64ac1eb227fea1f63a5c99", name: "chia seeds", quantity: "10g"}
1: {_id: "5f64ac1eb227fea1f63a5c9a", name: "cherries", quantity: "15g"}
2: {_id: "5f64ac1eb227fea1f63a5c9b", name: "honey", quantity: "30g"}
3: {_id: "5f64ac1eb227fea1f63a5c9c", name: "almond flour", quantity: "10g"}
4: {_id: "5f64ac1eb227fea1f63a5c9e", name: "almond flour", quantity: "10g"}
5: {_id: "5f64ac1eb227fea1f63a5c9f", name: "egg", quantity: "10g"}
6: {_id: "5f64ac1eb227fea1f63a5ca0", name: "unsalted butter", quantity: "30g"}
7: {_id: "5f64ac1eb227fea1f63a5ca1", name: "peanut butter", quantity: "50g"}
length: 8
__proto__: Array(0)
我希望的结果是不要有两次
名称:“杏仁粉”
,因为它已经在数组中,所以我可以只添加名称值不存在的对象:

而不是3:
{id:“5f64ac1eb227fea1f63a5c9c”,名称:“杏仁粉”,数量:“10g”},{id:“5f64ac1eb227fea1f63a5c9e”,名称:“杏仁粉”,数量:“10g”}
{id:“5f64ac1eb227fea1f63a5c9e”,名称:“杏仁粉”,数量:“20g”}

我想要实现的是,仅当每个成分不存在时,才将其添加到我的
groceryList
数组中,如果存在,则只将该成分的数量添加到现有的键中。例如,如果我有配料
{u id:“5f64ac1eb227fea1f63a5c99”,名称:“chia seeds”,数量:“10g”}
只添加数量,而不在我的
杂货店列表中创建新的配料对象

我不知道该采取什么办法。。如果你们有什么想法,我会非常感激的

**还有删除选项

onCheckRecipe(e) {
    if (e.detail.checked === true) {
      for (let recipe of this.loadedRecipes) {
        if (recipe.name === e.detail.value) {
          recipe.ingredients.forEach((eachIngredient) => {
            let matchedIng = this.groceryList.find(function (foundIng) {
              return foundIng.name === eachIngredient.name;
            });
            if (matchedIng) {
              matchedIng.quantity =
                matchedIng.quantity + eachIngredient.quantity;
            } else {
              this.groceryList.push(eachIngredient);
            }
          });
        }
      }
    } else {
      for (let recipe of this.loadedRecipes) {
        if (recipe.name === e.detail.value) {
          recipe.ingredients.forEach((element) => {
            let matched = this.groceryList.find(function (foundIngre) {
              return foundIngre.name === element.name;
            });
            if (matched.quantity === element.quantity) {
              let index = this.groceryList.findIndex(
                (x) => x.name === matched.name
              );
              this.groceryList.splice(index, 1);
            } else {
              matched.quantity = matched.quantity - element.quantity;
            }
          });
        }
      }
    }
  }

在配料数组上循环,并用于查找每个迭代的匹配项。如果存在,则更新数量,否则将Component对象推送到数组中

简化计算的一个建议是为单位添加另一个字段,并将数量存储为一个数字,这样您就不需要仅仅为了简单的数学运算而从字符串中解析单位

const配料=[{u id:“5f64ac1eb227fea1f63a5c9e”,名称:“杏仁粉”,数量:“10g”}];
成分。forEach(ingr=>{
//返回匹配的对象或未定义的对象
const wanted=data.find(({u id})=>\u id==ingr.\u id);
如果(需要){
const{quantity:wq}=需要,
单位=wq.匹配(/\D+/)[0];
通缉数量=parseInt(wq)+parseInt(ingr.quantity)+单位;
}否则{
数据推送(obj)
}
})
console.log(数据)
。作为控制台包装{最大高度:100%!重要;顶部:0;}

常量数据=[{u id:“5f64ac1eb227fea1f63a5c99”,名称:“chia种子”,数量:“10g”},
{u id:“5f64ac1eb227fea1f63a5c9a”,名称:“樱桃”,数量:“15g”},
{u id:“5f64ac1eb227fea1f63a5c9b”,名称:“蜂蜜”,数量:“30g”},
{u id:“5f64ac1eb227fea1f63a5c9e”,名称:“杏仁粉”,数量:“10g”},
{u id:“5f64ac1eb227fea1f63a5c9f”,名称:“鸡蛋”,数量:“10g”},
{u id:“5f64ac1eb227fea1f63a5ca0”,名称:“无盐黄油”,数量:“30g”},
{id:“5f64ac1eb227fea1f63a5ca1”,名称:“花生酱”,数量:“50g”}]

在配料数组上循环,并用于为每次迭代寻找匹配项。如果存在,则更新数量,否则将Component对象推送到数组中

简化计算的一个建议是为单位添加另一个字段,并将数量存储为一个数字,这样您就不需要仅仅为了简单的数学运算而从字符串中解析单位

const配料=[{u id:“5f64ac1eb227fea1f63a5c9e”,名称:“杏仁粉”,数量:“10g”}];
成分。forEach(ingr=>{
//返回匹配的对象或未定义的对象
const wanted=data.find(({u id})=>\u id==ingr.\u id);
如果(需要){
const{quantity:wq}=需要,
单位=wq.匹配(/\D+/)[0];
通缉数量=parseInt(wq)+parseInt(ingr.quantity)+单位;
}否则{
数据推送(obj)
}
})
console.log(数据)
。作为控制台包装{最大高度:100%!重要;顶部:0;}

常量数据=[{u id:“5f64ac1eb227fea1f63a5c99”,名称:“chia种子”,数量:“10g”},
{u id:“5f64ac1eb227fea1f63a5c9a”,名称:“樱桃”,数量:“15g”},
{u id:“5f64ac1eb227fea1f63a5c9b”,名称:“蜂蜜”,数量:“30g”},
{u id:“5f64ac1eb227fea1f63a5c9e”,名称:“杏仁粉”,数量:“10g”},
{u id:“5f64ac1eb227fea1f63a5c9f”,名称:“鸡蛋”,数量:“10g”},
{u id:“5f64ac1eb227fea1f63a5ca0”,名称:“无盐黄油”,数量:“30g”},
{id:“5f64ac1eb227fea1f63a5ca1”,名称:“花生酱”,数量:“50g”}]

这是我的解决方案,希望对您有所帮助:

const data = [
  { _id: "5f64ac1eb227fea1f63a5c99", name: "chia seeds", quantity: "10g" },
  { _id: "sdf7687hkksjj8881zzaooPP", name: "peanut butter", quantity: "20g" },
  { _id: "abcf781eb227fea1f63a5c9a", name: "cherries", quantity: "15g" },
  { _id: "5f64ac1eb227fea1f63a5c9b", name: "honey", quantity: "30g" },
  { _id: "5f64ac1eb227fea1f63a5c9c", name: "almond flour", quantity: "10g" },
  { _id: "5f64ac1eb227fea1f63a5c9e", name: "almond flour", quantity: "10g" },
  { _id: "5f64ac1eb227fea1f63a5c9f", name: "egg", quantity: "10g" },
  { _id: "ghg990c1eb227fea1f63a5c7", name: "egg", quantity: "19g" },
  { _id: "dsfsdffer340fea1f63a5ca0", name: "unsalted butter", quantity: "30g" },
  { _id: "5f64ac1eb227fea1f63a5ca1", name: "peanut butter", quantity: "50g" }
];

const groceryList = data.reduce(
  (acc, value) => {
    const index = acc.findIndex(elt => elt.name === value.name);
    if (index !== -1) {
      const accQuantity = acc[index].quantity;
      const valueQuantity = value.quantity;
      const unit = accQuantity.match(/\D+/)[0];
      acc[index].quantity = +accQuantity.match(/\d+/)[0] + +valueQuantity.match(/\d+/)[0] + unit;
    } else {
      acc.push(value);
    }
    return acc;
  }, []
);
console.log(groceryList);

这是我的解决方案,我希望这会有所帮助:

const data = [
  { _id: "5f64ac1eb227fea1f63a5c99", name: "chia seeds", quantity: "10g" },
  { _id: "sdf7687hkksjj8881zzaooPP", name: "peanut butter", quantity: "20g" },
  { _id: "abcf781eb227fea1f63a5c9a", name: "cherries", quantity: "15g" },
  { _id: "5f64ac1eb227fea1f63a5c9b", name: "honey", quantity: "30g" },
  { _id: "5f64ac1eb227fea1f63a5c9c", name: "almond flour", quantity: "10g" },
  { _id: "5f64ac1eb227fea1f63a5c9e", name: "almond flour", quantity: "10g" },
  { _id: "5f64ac1eb227fea1f63a5c9f", name: "egg", quantity: "10g" },
  { _id: "ghg990c1eb227fea1f63a5c7", name: "egg", quantity: "19g" },
  { _id: "dsfsdffer340fea1f63a5ca0", name: "unsalted butter", quantity: "30g" },
  { _id: "5f64ac1eb227fea1f63a5ca1", name: "peanut butter", quantity: "50g" }
];

const groceryList = data.reduce(
  (acc, value) => {
    const index = acc.findIndex(elt => elt.name === value.name);
    if (index !== -1) {
      const accQuantity = acc[index].quantity;
      const valueQuantity = value.quantity;
      const unit = accQuantity.match(/\D+/)[0];
      acc[index].quantity = +accQuantity.match(/\d+/)[0] + +valueQuantity.match(/\d+/)[0] + unit;
    } else {
      acc.push(value);
    }
    return acc;
  }, []
);
console.log(groceryList);

请提供一些示例输入数据和预期结果。您还收到了哪些错误?出什么事了?您提供的一组代码没有真正的技术问题陈述,或者您在代码中的具体位置有issuesHi@charlietfl,请道歉。我只是补充了一点信息和期望的结果。谢谢你提醒我!那么,结果应该是什么呢?将数量更改为“20g”?“杏仁粉”的所有id不都应该相同吗?是的,并且只保留一个名为“杏仁粉”的obj。而不是3:{u id:“5f64ac1eb227fea1f63a5c9c”,名称:“杏仁粉”,数量:“10g”}4:{u id:“5f64ac1eb227fea1f63a5c9e”,名称:“杏仁粉”,数量:“10g”}将是4:{u id:“5f64ac1eb227fea1f63a5c9e”,名称:“杏仁粉”,数量:“20g”}请提供一些样本输入数据和预期结果。您还收到了哪些错误?出什么事了?您提供的一组代码没有真正的技术问题陈述,或者您在代码中的具体位置有issuesHi@charlietfl,请道歉。我只是补充了一点信息和期望的结果。谢谢你提醒我!那么,结果应该是什么呢?将数量更改为“20g”?“杏仁粉”的所有id不都应该相同吗?是的,并且只保留一个名为“杏仁粉”的obj。而不是3:{id:“5f64ac1eb227fea1f63a5c9c”,名称:“杏仁粉”,数量:“10g”}4:{id:“5f64ac1eb227fea1f63a5c9e”,名称:“杏仁粉”,数量:“10g”}将是4:{id:“5f64ac1eb227fea1f63a5c9e”,名称:“杏仁粉”,数量:“20g”}嗨!非常感谢你。。我试图理解你的代码,因为它的语法是我从未使用过的,我刚刚开始学习编码,并试图弄明白如何使它适应我的方法,因为我不想使用JU