Home > Blockchain >  Filter an object containing a key that contains an array of objects using an array of strings
Filter an object containing a key that contains an array of objects using an array of strings

Time:08-21

I have a JSON file that contains over 300 objects of different kinds of drinks and their ingredients, instructions, names, etc...

Here is the layout of one of the objects:

    "strCategory": "Beer",
    "strDrink": "110 in the shade",
    "strAlcoholic": "Alcoholic",
    "dateModified": "2016-02-03 14:51:57",
    "strInstructionsIT": "Riempi un bicchierino di tequila.\r\nRiempi un boccale di birra con la birra chiara.\r\nMetti il bicchierino nella birra e bevi velocemente.",
    "idDrink": "15423",
    "strDrinkThumb": "https://www.thecocktaildb.com/images/media/drink/xxyywq1454511117.jpg",
    "strCreativeCommonsConfirmed": "No",
    "strGlass": "Beer Glass",
    "Ingredients": [
      {
        "Ingredient": "Lager",
        "Measurement": "16 oz "
      },
      {
        "Measurement": "1.5 oz ",
        "Ingredient": "Tequila"
      }
    ],
    "strInstructionsDE": "Shooter tröpfchenweise in ein Glas geben. Mit Bier füllen.",
    "strInstructions": "Drop shooter in glass. Fill with beer"
  },
  {
    "Ingredients": [
      {
        "Ingredient": "Malibu Rum",
        "Measurement": "1/2 oz "
      },
      {
        "Ingredient": "Light Rum",
        "Measurement": "1/2 oz "
      },
      {
        "Ingredient": "Rum",
        "Measurement": "1/2 oz"
      },
      {
        "Measurement": "1 oz ",
        "Ingredient": "Dark Creme De Cacao"
      },
      {
        "Measurement": "1 oz ",
        "Ingredient": "Cointreau"
      },
      {
        "Ingredient": "Milk",
        "Measurement": "3 oz "
      },
      {
        "Ingredient": "Coconut Liqueur",
        "Measurement": "1 oz "
      },
      {
        "Measurement": "1 cup ",
        "Ingredient": "Vanilla Ice-cream"
      }
    ],
    "strInstructionsDE": "Alle Zutaten vermengen. Mischen, bis alles glatt ist. Auf Wunsch mit Schokoladenraspeln garnieren.",
    "strDrinkThumb": "https://www.thecocktaildb.com/images/media/drink/rvwrvv1468877323.jpg",
    "strCategory": "Shake",
    "strInstructions": "Combine all ingredients. Blend until smooth. Garnish with chocolate shavings if desired.",
    "idDrink": "14588",
    "strCreativeCommonsConfirmed": "No",
    "strInstructionsIT": "Combina tutti gli ingredienti.\r\nFrulla fino a che è liscio.\r\nGuarnire con scaglie di cioccolato se lo si desidera.",
    "dateModified": "2016-07-18 22:28:43",
    "strGlass": "Beer Mug",
    "strDrink": "151 Florida Bushwacker",
    "strAlcoholic": "Alcoholic"
  }

Please bring your attention to the key "Ingredients".

I want to use an array that perhaps looks something like this:

let Ingredients_Entered_By_User = ["Vodka", "Rum", "Lemonade"];

I don't want to find just any of those ingredients in one of these objects, I want objects that contain all of these ingredients. Not strictly, but I want those ingredients to be in all drink objects filtered.

CodePudding user response:

Use some array methods

const data = [{
    "Ingredients": [
      {
        "Ingredient": "Malibu Rum",
        "Measurement": "1/2 oz "
      },
      {
        "Ingredient": "Light Rum",
        "Measurement": "1/2 oz "
      },
      {
        "Ingredient": "Rum",
        "Measurement": "1/2 oz"
      },
      {
        "Measurement": "1 oz ",
        "Ingredient": "Dark Creme De Cacao"
      },
      {
        "Measurement": "1 oz ",
        "Ingredient": "Cointreau"
      },
      {
        "Ingredient": "Milk",
        "Measurement": "3 oz "
      },
      {
        "Ingredient": "Coconut Liqueur",
        "Measurement": "1 oz "
      },
      {
        "Measurement": "1 cup ",
        "Ingredient": "Vanilla Ice-cream"
      }
    ],
    "strInstructionsDE": "Alle Zutaten vermengen. Mischen, bis alles glatt ist. Auf Wunsch mit Schokoladenraspeln garnieren.",
    "strDrinkThumb": "https://www.thecocktaildb.com/images/media/drink/rvwrvv1468877323.jpg",
    "strCategory": "Shake",
    "strInstructions": "Combine all ingredients. Blend until smooth. Garnish with chocolate shavings if desired.",
    "idDrink": "14588",
    "strCreativeCommonsConfirmed": "No",
    "strInstructionsIT": "Combina tutti gli ingredienti.\r\nFrulla fino a che è liscio.\r\nGuarnire con scaglie di cioccolato se lo si desidera.",
    "dateModified": "2016-07-18 22:28:43",
    "strGlass": "Beer Mug",
    "strDrink": "151 Florida Bushwacker",
    "strAlcoholic": "Alcoholic"
  }]

const ingredients = ["Vodka", "Rum", "Lemonade"]

const items = data.filter(coctail => ingredients.every(ing => coctail.Ingredients.find(cocIng => cocIng.Ingredient === ing)))

console.log(items)

  • Related