简体   繁体   English

使用字符串数组过滤包含包含对象数组的键的 object

[英]Filter an object containing a key that contains an array of objects using an array of strings

I have a JSON file that contains over 300 objects of different kinds of drinks and their ingredients, instructions, names, etc...我有一个 JSON 文件,其中包含 300 多种不同种类的饮料及其成分、说明、名称等对象...

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.不严格,但我希望这些成分在所有饮料对象中都经过过滤。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM