I have the following list of ingredients in an array called myBar. The ingredient is contained in the attribute “name”
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", }, ]
I also have a second array called cocktailList containing a list of cocktail recipes. In here the ingredients are contained within the field “ingredients”.
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", } ]
I’m trying to check what cocktails I can make based on a match of the content of the ingredients. The idea is to iterate each recipe (in cocktailList) and check that ALL ingredients are also part of my list of ingredients in myBar.
For example “drinkId”: “1102” should return true since I have all ingredients (vodka and orange juice) in myBar while “drinkId”: “1101” should return false since I don’t have neither lime syrup nor lime.
Advertisement
Answer
Create an array of ingredients names from first array, so that you can quickly search for an ingredient in direct array.
// 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"]
Now loop through cocktailList
and for each ingredient search in arrName
. If count of matching element is equal to count of ingredients in cocktail then its a match.
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" } ]