1
  Array.forEach((element) => {
    recipes.forEach((curr, index) => {
      found = true;
      item = element;
      if (!curr.ingredients.includes(element)) found = false;
      if (index + 1 === recipes.length && found === false) {
        console.log(element, false);
      } else if (index + 1 === recipes.length && found === true) {
        console.log(element, true);
        foundItem = element;
      }
    });
  });
  return foundItem;

Hello, guys... I am trying to debug such code... What is happening is that, as you can see in the picture down below, My code is returning me "açúcar", "calabresa", and "cerveja". Image where you can see the output from my console

However, it is not what I mean since I am looping through a JSON list that happens to have ingredients for several different recipes... Such as follows

  "recipes": [
    {
      "id": "00832498-3acc-4bde-9d89-77eacef26680",
      "title": "Excelente pudim de milho",
      "ingredients": [
        "abóbora",
        "aveia",
        "azeite",
        "açúcar",
        "cerveja",
        "frango",
        "milho",
        "morango",
        "vinho"
      ],
      "price": 78.53
    },
    {
      "id": "008b345d-8f7c-427d-988b-4f238b8315f2",
      "title": "Excelente bolo de azeite",
      "ingredients": [
        "abóbora",
        "aveia",
        "azeite",
        "açúcar",
        "frango",
        "vinho"
      ],
      "price": 82.55
    },
    {
      "id": "00d14194-f223-447e-86a0-b535c451d53f",
      "title": "Exuberante coxinha de vinho",
      "ingredients": [
        "abóbora",
        "azeite",
        "açúcar",
        "calabresa",
        "cerveja",
        "chuchu",
        "frango",
        "morango",
        "uva",
        "vinho"
      ],

What I want to get is açucar, since is the only ingredient which is present in all of the recipes... As for me, it seems like there is some sort of cache-related problem here

2
  • Could you explain are you trying to do with that code? Commented Mar 12, 2021 at 16:58
  • Ok, @GuillermoGarcíaLópez, I can... I am trying to find some ingredient that is present in all of the recipes... The recipes are in the JSON file
    – lucasbbs
    Commented Mar 12, 2021 at 17:00

2 Answers 2

1

Maybe you could change your code to this:

Array.forEach((element) => {
  const isIncluded = recipes.every(recipe => recipe.ingredients.includes(element));
  if (isIncluded) {
    // Do something with the ingredient or the element.
  }
});
return foundItem;

The every method checks that every recipe includes the ingredient that you pass to it.

1

if you have access to lodash you can make this really clean

import { intersection } from 'lodash'

getCommonIngredients = (recipes) => {
  const ingredients = recipes.map(recipe => recipe.ingredients)
  return intersection(...ingredients)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.