【问题标题】:Matching 2 arrays based on the values of some fields根据某些字段的值匹配 2 个数组
【发布时间】:2020-09-23 16:23:04
【问题描述】:

我在一个名为 myBar 的数组中有以下成分列表。成分包含在属性“名称”中

myBar:  Array [
  bar {
    "category": "spirits",
    "id": "1",
    "ingredientId": "2003",
    "name": "vodka",
  },
  bar {
    "category": "juice",
    "id": "2",
    "ingredientId": "2017",
    "name": "orange juice",
  },
  bar {
    "category": "juice",
    "id": "3",
    "ingredientId": "2020",
    "name": "lemon juice",
  },
  bar {
    "category": "juice",
    "id": "4",
    "ingredientId": "2027",
    "name": "Pineapple Juice",
  },
  bar {
    "category": "juice",
    "id": "5",
    "ingredientId": "2018",
    "name": "apple Juice",
  },
  bar {
    "category": "juice",
    "id": "6",
    "ingredientId": "2025",
    "name": "Lime Juice",
  },
  bar {
    "category": "spirits",
    "id": "7",
    "ingredientId": "2001",
    "name": "gin",
  },
  bar {
    "category": "spirits",
    "id": "8",
    "ingredientId": "2005",
    "name": "whiskey",
  },
  bar {
    "category": "spirits",
    "id": "9",
    "ingredientId": "2002",
    "name": "rum",
  },
]

我还有一个名为 cocktailList 的数组,其中包含鸡尾酒配方列表。在这里,成分包含在“成分”字段中。

cocktailList:  Array [
Object {
    "alcoholic": "true",    
    "drinkId": "1101", 
    "ingredients": " gin, lime syrup, lime",
},
Object {
    "alcoholic": "true",    
    "drinkId": "1102", 
    "ingredients": "vodka, orange juice",
},
Object {
    "alcoholic": "true",    
    "drinkId": "1103", 
    "ingredients": "rum, coke",
},
Object {
    "alcoholic": "true",    
    "drinkId": "1104", 
    "ingredients": "sweet vermouth, campari, prosecco",
},
Object {
    "alcoholic": "true",    
    "drinkId": "1105", 
    "ingredients": "Gin, Olive Juice, Olives, Dry vermouth",
},
Object {
    "alcoholic": "true",    
    "drinkId": "1106", 
    "ingredients": " Vodka, Triple Sec, Lime Juice",
},
Object {
    "alcoholic": "true",    
    "drinkId": "1107", 
    "ingredients": " vodka, Raspberry Liqueur, Pineapple Juice",
}
]

我正在尝试根据成分含量的匹配来检查我可以制作哪些鸡尾酒。 这个想法是迭代每个食谱(在 cocktailList 中)并检查所有成分是否也是我在 myBar 中的成分列表的一部分。

例如 "drinkId": "1102" 应该返回 true,因为我在 myBar 中有所有成分(伏特加橙汁),而"drinkId": "1101" 应该返回 false,因为我既没有酸橙糖浆也没有酸橙。

【问题讨论】:

    标签: javascript arrays json object


    【解决方案1】:

    从第一个数组创建一个成分名称数组,以便您可以快速搜索直接数组中的成分。

    // arr1 is your list of ingredients in your bar.
    var arrName  = [];
    arr1.forEach(element => arrName.push(element.name));
    
    console.log(arrName);
    Output:
    ["vodka", "orange juice", "lemon juice", "Pineapple Juice", "apple Juice", "Lime Juice", "gin", "whiskey", "rum"]
    

    现在循环遍历cocktailList 并在arrName 中搜索每种成分。如果匹配元素的数量等于鸡尾酒中成分的数量,则它是匹配的。

    var finalArr = arr2.filter(function(obj){
        var arrIngredient = obj.ingredients.split(',');
        var intMatch = 0;
        
        arrIngredient.forEach(element => arrName.indexOf(element.trim()) >= 0 ? intMatch++ : '');
        
        return arrIngredient.length == intMatch;
    });
    
    console.log(finalArr);
    Output:
    [
        {
             alcoholic: "true"
             drinkId: "1102"
             ingredients: "vodka, orange juice"
        }
    ]
    

    【讨论】:

    • 非常感谢,效果很好。我只需要在匹配之前将 .toLowerCase() 添加到两个数组输出中。如果我只想让一种成分成为配方的一部分,我应该使用 .some 而不是 indexOf?
    猜你喜欢
    • 2018-10-15
    • 1970-01-01
    • 2012-05-15
    • 2018-09-08
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多