【问题标题】:How to check if an array contains any value of another array如何检查一个数组是否包含另一个数组的任何值
【发布时间】:2020-09-21 13:27:37
【问题描述】:

我有一个对象数组,需要检查第二个数组的任何值是否包含在特定字段中。

我的数组名为 cocktailList,我需要检查的字段名为“ingredients”。

第二个数组称为selectedOptions

当第二个数组只有 1 个值时,以下代码可以完美运行。

例子

const selectedOptions = ["vodka"];
const selectedDrinks = cocktailList.filter(function (item, i) {
      if (item.ingredients.toLowerCase().includes(selectedOptions)) {
        return true;
      } else false;
    });

当我在 selectedOptions 中有多个值时会出现问题 如果 item.ingredients 包含 selectedOptions 的任何值,我的目标是返回 TRUE。

如果我有以下情况:

const selectedOptions = ["vodka", "rum"];

只有在 ingredients 字段包含 selectedOptions

的所有值时才会返回 true

我已经按照其他人的建议尝试了以下解决方案

if (item.ingredients.toLowerCase().some((val) => selectedOptions.indexOf(val) !== -1))

但我遇到了一个错误

TypeError: item.ingredients.toLowerCase().some is not a function. (In 'item.ingredients.toLowerCase().some(function (i)

每个项目都是一个对象,其中包含包含此类字符串的“成分”字段

 Object {
    "alcoholic": "true",
    "complete": false,
    "country": "cuba",
    "dairy_free": "",
    "description": "Made without the traditional orange liqueur",
    "difficulty": "",
    "dosage": "1½ shot(s) Tequila, ½ shot(s) Lime Juice, ½ shot(s)",
    "drinkId": "1103",
    "equipment": "Shaker, Strainer",
    "garnish": "",
    "glass": "collins",
    "ingredients": "Tequila, Lime Juice, Agave Nectar Juice",
}

【问题讨论】:

  • 似乎问题在 JavaScript 中 - 请标记问题。
  • 请告诉我们cocktailList的值。看起来在原始代码中,item.ingredients 是一个字符串,但在另一个示例中它不是?
  • @Bergi 我已经更新了我的问题,并在底部包含了一个对象示例。成分字段确实是一个字符串。
  • 啊。你会想要使用selectedOptions.some(…),而不是ingredients.….some(…)
  • @Bergi 这样你的意思是const selectedDrinks = cocktailList.filter(function (item, i) { if (selectedOptions.some(item.ingredients.toLowerCase())) { return true; } else false; });

标签: javascript arrays prototype array.prototype.map


【解决方案1】:

您可以使用some

从@ikik 借用sn-p:

let cocktailListc = [{
    "ingredients": "vodka"
  },
  {
    "ingredients": "water"
  },
  {
    "ingredients": "soda"
  },
  {
    "ingredients": "rum"
  },
  {
    "ingredients": "orange"
  }
]
const selectedOptions = ["vodka", "rum", "limun"];

console.log(cocktailListc.some(item => selectedOptions.includes(item.ingredients)))

但是会失败

let cocktailListc = [{
    "ingredients": "vodka"
  },
  {
    "ingredients": "water"
  },
  {
    "ingredients": "soda"
  },
  {
    "ingredients": "rum"
  },
  {
    "ingredients": "orange"
  }
]
const selectedOptions = ["a", "b", "c"];

console.log(cocktailListc.some(item => selectedOptions.includes(item.ingredients)))

【讨论】:

    【解决方案2】:

    你可以做简单的双循环...

    function test() {
      let cocktailListc = [{
          "ingredients": "Tequila, Lime Juice, Agave Nectar Juice, Russian Vodka"
        },
        {
          "ingredients": "water test"
        },
        {
          "ingredients": "soda test"
        },
        {
          "ingredients": "rum test"
        },
        {
          "ingredients": "orange test"
        }
      ]
    
      const selectedOptions = ["vodka", "rum", "limun"];
    
      let check = [];
    
      selectedOptions.forEach(opt => {
        cocktailListc.forEach(cocktail => {
          if (cocktail.ingredients.toLowerCase().includes(opt.toLowerCase()) === true) {
            check.push(cocktail);
          }
        });
      });
    
      if (check.length > 0) {
        console.log(true)
         console.log(check)
      }
    }
    test()

    【讨论】:

    • 您假设 selectedOptions 的值必须等于成分的值,但事实并非如此。示例 "ingredients": "Tequila, Lime Juice, Agave Nectar Juice, Russian Vodka"selectedOptions = ["vodka", "rum", "whiskey"]
    • @Butri 在看到您的编辑后,我刚刚更改了该设置,轻松修复:if (cocktail.ingredients.includes(opt) === true) {
    • 我认为我们走在正确的道路上,但是您的解决方案没有返回我需要的东西。如果您检查我的代码,我会在每次匹配时添加鸡尾酒对象,因此我认为我们不应该使用check.push(opt)。澄清一下,我需要以 let check = []; 结尾,其中包含字段成分包含 selectedOptions 值之一的所有对象。
    • @Butri 哦,我明白了,但只需切换它并推送check.push(cocktail);cocktail 对象而不是opt(一个选项)。编辑小提琴。
    【解决方案3】:

    你可以这样做:

    var selectedOptions = ["vodka"];
    var cocktailList = [{ ingredients: "vodka, lemon, sugar" }];
    
    var selectedDrinks = cocktailList.filter((cocktail, i) =>
      selectedOptions.reduce(
        (has, option) => has && cocktail.ingredients.includes(option),
        true
      )
    );
    

    【讨论】:

      猜你喜欢
      • 2011-01-16
      • 1970-01-01
      • 2017-05-30
      • 2022-01-09
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多