在两个不同的数组中找到匹配项 - javaScript

时间:2016-04-26 20:07:27

标签: javascript foreach

我正在尝试写一个条件,可以找到食谱是否适合素食者。 如果不是素食主义者,我想控制食谱数组以及消息(非素食主义者)。

我尝试了几个东西,两个for循环,现在我正在尝试使用forEach和includes。问题是我的逻辑出了问题,不打印非素食信息,而且,我正在打印食谱3次。这是我唯一能想到的,因为我必须有一个素食项目的循环。

我这里有一个bin。有点乱。



var cookbook = [noodlesWithChicken, bakedPotatoWithBroccoli, hamPineapplePitaPizza, shrimpOliveSalad, chocolateBananaSundae];

var noodlesWithChicken = ["noodles", "chicken", "carrots", "cucumber", "peanut butter", "soy sauce" ];

var bakedPotatoWithBroccoli = ["russet potatos", "broccolli", "butter", "salt", "sour cream"];

var hamPineapplePitaPizza = ["pitas", "mozzarella", "pineaples", "ham"];

var shrimpOliveSalad = ["shrimp", "lettuce", "tomatoes", "artichoke", "black olives", "mayo", "chili sauce"];

var chocolateBananaSundae = ["bananas", "vanilla ice cream", "chocolate sauce", "shredded coconut"];


var vegetarians = ["chicken", "ham", "shrimp"];
var veganItems = ["chicken", "ham", "shrimp", "mozzarella", "sour cream", "mayo", "vanilla ice cream"];

cookbook.forEach(function(item){
  for(var i = 0; i < vegetarians.length; i++){
    if(cookbook.includes(vegetarians[i])){
      console.log(item + " (non-vegetarian)");
    } else {
      console.log(item);
    }
  }
});
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

你的代码似乎很好(不是循环),问题是你宣布所有的烹饪书&#39;在包含它们的数组之后:

我所做的就是在开始循环之前将阵列向下移动,然后我也改变了循环......你现在拥有它的方式并没有完全发挥作用:

cookbook.forEach(function(book){
  console.log(book);
  vegetarians.forEach(function(veg) {
    if (book.indexOf(veg) > -1) {
      console.log('contains meat');
    }
  });
});

查看这个新Bin:http://jsbin.com/bimafo/2/edit?js,console

答案 1 :(得分:0)

由于您正在执行cookbook.includes(vegetarians[i])而不是item.includes(vegetarians[i]),因此不打印该邮件。您要检查的item包含非素食选项。

你在forEach内循环3次,每次非素食选项一次。这就是为什么每item输出3次。

答案 2 :(得分:0)

正如其他人所指出的,你需要在声明其子代后声明你的cookbook数组。

我还想指出,当你可能想要调用cookbook.includes时,你的for循环正在调用item.includes

此外,您正在记录肉类列表中食谱和物品的每个组合的结果,当您可能想要做的是每个食谱记录一次,如果食谱中有任何肉类。您可以使用以下内容轻松完成此操作:

cookbook.forEach(function(item){
  var hasMeat = vegetarians.some(item.includes.bind(item));
  console.log(item, hasMeat ? "is not vegetarian" : "is vegetarian");
});

var noodlesWithChicken = ["noodles", "chicken", "carrots", "cucumber", "peanut butter", "soy sauce" ];

var bakedPotatoWithBroccoli = ["russet potatos", "broccolli", "butter", "salt", "sour cream"];

var hamPineapplePitaPizza = ["pitas", "mozzarella", "pineaples", "ham"];

var shrimpOliveSalad = ["shrimp", "lettuce", "tomatoes", "artichoke", "black olives", "mayo", "chili sauce"];

var chocolateBananaSundae = ["bananas", "vanilla ice cream", "chocolate sauce", "shredded coconut"];

var cookbook = [noodlesWithChicken, bakedPotatoWithBroccoli, hamPineapplePitaPizza, shrimpOliveSalad, chocolateBananaSundae];

var vegetarians = ["chicken", "ham", "shrimp"];
var veganItems = ["chicken", "ham", "shrimp", "mozzarella", "sour cream", "mayo", "vanilla ice cream"];

cookbook.forEach(function(item){
  var hasMeat = vegetarians.some(item.includes.bind(item));
  console.log(item, hasMeat ? "is not vegetarian" : "is vegetarian");
});