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

finding a match in two different arrays - javaScript

本文关键字:查找 javaScript 数组 两个      更新时间:2023-09-26

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

我尝试了几件事,两个 for 循环,现在我正在尝试使用 forEach 和包含。 问题是我的逻辑有问题,没有打印非素食信息,而且,我打印了 3 次食谱。这是我唯一能想到的,因为我也必须为素食提供一个循环。

我这里有一个垃圾箱。有点乱。

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);
    }
  }
});

你的代码看起来很好(不是循环),问题是你在包含它们的数组之后声明了所有的"说明书":

我所做的只是在你开始循环之前将数组向下移动,我也稍微改变了循环......您现在拥有它的方式不太有效:

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

看看这个新的垃圾箱:http://jsbin.com/bimafo/2/edit?js,console

它不会打印消息,因为您正在执行cookbook.includes(vegetarians[i])而不是item.includes(vegetarians[i])。这是您要检查包含非素食选项的item

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

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

我还想指出,当您可能想调用item.includes时,您的 for 循环正在调用cookbook.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");
});