繁体   English   中英

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

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

我有一个 JSON 文件,其中包含 300 多种不同种类的饮料及其成分、说明、名称等对象...

这是其中一个对象的布局:

    "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"
  }

请注意关键的“成分”。

我想使用一个可能看起来像这样的数组:

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

我不想在其中一个对象中找到任何这些成分,我想要包含所有这些成分的对象。 不严格,但我希望这些成分在所有饮料对象中都经过过滤。

使用一些数组方法

 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