-2

I have a recipes array filled with objects. An object looks like this

var recipes = [
  {
    name: 'All-Purpose Rub for Meat',
    ingredients: [
      {
        name: 'white sugar',
        quantity: {
          number: 4,
          unit: 'tablespoon'
        },
      },
      {
        name: 'salt',
        quantity: {
          number: 4,
          unit: 'tablespoon'
        }
      },
      {
        name: 'paprika',
        quantity: {
          number: 1,
          unit: 'tablespoon'
        }
      },
      {
        name: 'mustard powder',
        quantity: {
          number: 2,
          unit: 'tablespoon'
        }
      },
      {
        name: 'ground black pepper',
        quantity: {
          number: .5,
          unit: 'teaspoon'
        }
      },
      {
        name: 'dried oregano',
        quantity: {
          number: 1,
          unit: 'pinch'
        }
      },
      {
        name: 'dried thyme',
        quantity: {
          number: 1,
          unit: 'pinch'
        }
      },
    ],
    servingSize: {
      quantity: 12,
      unit: 'servings'
    },
    // usedFor: [ribs, chicken, pork, fish, beef]
  },


  {
    name: 'Rosemary Garlic Rub',
    ingredients: [{
      name: 'ground black pepper',
      quantity: {number: 1, unit: 'tablespoon'}},
      {
      name: 'kosher salt',
      quantity: {number: 1, unit: 'tablespoon'}},
      {
      name: 'chopped fresh rosemary',
      quantity: {number: 3, unit: 'tablespoon'}},
      {
      name: 'dried rosemary',
      quantity: {number: 1, unit: 'tablespoon'}},
      {
      name: 'diced garlic',
      quantity: {number: 8, unit: 'cloves'}},
      {
      name: 'olive oil',
      quantity: {number: .333, unit: 'cup'}}            
        ],
    servingSize: {quantity: 4, unit: 'servings'}, 
    // usedFor: [chicken, pork] 
  },
]

What I want to do is filter the recipes array based on an inputted spice, so it would be something like this

filterRecipes({name: 'ground black pepper'});

I've tried

function filterRecipes(spice) {
 let filteredarray = recipes.filter((recipe) => {
    recipe.ingredients.includes(spice.name);
 });
 return filteredarray;
}

console.log(filterRecipes({name: 'kosher salt'}))

The filterRecipes function would then return an array of all recipes where it had the ingredient ' ground black pepper.

Any help would be greatly appreciated.

It looked like I would need something similar to his, but I cant use some because I need all I dont know why its flagging the below as the correct answer, but that doesnt give me back all the items with the ingredients

Filtering array of objects by searching nested object properties

It should return an array of recipes with only objects that have the ingredient that was put as a parameter

3
  • Possible duplicate of Filtering array of objects by searching nested object properties Commented Mar 31, 2019 at 21:28
  • 1
    Your recipes.filter function does not return anything, I think once you fix that it should work. Commented Mar 31, 2019 at 21:32
  • @GeorgeJempty I included that in my question that I couldnt use that Commented Mar 31, 2019 at 21:37

1 Answer 1

0

includes() is not the right choice here, try some() instead for your filterRecipes function:

function filterRecipes(recipes, ingredient) {
  return recipes.filter(({ ingredients }) => {
    return ingredients.some(({ name }) => name === ingredient);
  });
}

...and then call it with ingredient name, not the object:

filterRecipes(recipes, 'kosher salt');

You can learn more about .some() on MDN

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.