0

const menu = [{
    name: "tofu fritters",
    ingredients: ["tofu", "egg yolk", "breadbrumbs", "paprika"],
  },
  {
    name: "black bean curry",
    ingredients: ["black beans", "garam masala", "rice"],
  },
  {
    name: "chocolate tiffin",
    ingredients: [
      "dark chocolate",
      "egg",
      "flour",
      "brown sugar",
      "vanilla essence",
    ],
  },
  {
    name: "hummus",
    ingredients: ["chickpeas", "tahini", "lemon", "garlic", "salt"],
  },
];

searchResult = menu.some(menuItem => menuItem.ingredients === 'flour');
console.log(searchResult);

I was expecting this to return true since flour is present in the array for the third menu item but it returns false. Some() only seems to return true if I remove the array entirely from the object.

dev.skas
  • 522
  • 2
  • 16
Paul Bevan
  • 15
  • 4

2 Answers2

1

As menuItem.ingredients is an array, try with menuItem.ingredients.includes('flour') instead of menuItem.ingredients === 'flour'

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

const menu = [{
    name: "tofu fritters",
    ingredients: ["tofu", "egg yolk", "breadbrumbs", "paprika"],
  },
  {
    name: "black bean curry",
    ingredients: ["black beans", "garam masala", "rice"],
  },
  {
    name: "chocolate tiffin",
    ingredients: [
      "dark chocolate",
      "egg",
      "flour",
      "brown sugar",
      "vanilla essence",
    ],
  },
  {
    name: "hummus",
    ingredients: ["chickpeas", "tahini", "lemon", "garlic", "salt"],
  },
];

searchResult = menu.some(menuItem => menuItem.ingredients.includes('flour'));
console.log(searchResult);
ksav
  • 20,015
  • 6
  • 46
  • 66
-1

menuItem is an Object menuItem.ingredients is an array do we can not directly equalize to a variable, but we can check if it contains the variable so menuItem.ingredients.includes('flour') work for this

const menu = [{
    name: "tofu fritters",
    ingredients: ["tofu", "egg yolk", "breadbrumbs", "paprika"],
  },
  {
    name: "black bean curry",
    ingredients: ["black beans", "garam masala", "rice"],
  },
  {
    name: "chocolate tiffin",
    ingredients: [
      "dark chocolate",
      "egg",
      "flour",
      "brown sugar",
      "vanilla essence",
    ],
  },
  {
    name: "hummus",
    ingredients: ["chickpeas", "tahini", "lemon", "garlic", "salt"],
  },
];

searchResult = menu.some(menuItem => menuItem.ingredients.includes('flour') );
console.log(searchResult);
dev.skas
  • 522
  • 2
  • 16