First App || Return only content that matches this array

Im trying to create my first application. It would check what cocktails you can make from available spirits.

It was going relatively well untill today where I hit a wall.
I have an availabeSpirits array which all of user added spirits are pushed in. What I want to do now, is to compare strings in availableSpirits with an array that has the cocktailList. I want to return all cocktails that match strings in availableSpirits.

const cocktailList = [ { name: 'sampke cocktail1', spirit: ['pimms'] }, { name: 'sample cocktail2, spirit: ['rum', 'vodka', 'gin'] }, { name: 'sample cocktail3', spirit: ['vodka', 'rum',] }

availableSpirits = ['vodka', 'rum', 'pimms']

With given spirits, only cocktail #1 and #3 should be returned…

.include() will only work if availableSpirits has only one spirit,
.filter() seems to be the right way to go, but im not sure how to implement it…

I would be grateful for any advice on how to approach this!

I can’t give you an advice, I can only give you solution.

const cocktailList = [
  { name: 'sample cocktail1', spirit: ['pimms'] },
  { name: 'sample cocktail2', spirit: ['rum', 'vodka', 'gin'] },
  { name: 'sample cocktail3', spirit: ['vodka', 'rum'] },
];

const availableSpirits = ['vodka', 'rum', 'pimms'];

const recipes = cocktailList.filter(recipe =>
  recipe.spirit.every(spirit => availableSpirits.includes(spirit))
);

console.log(recipes);

Have you tried working through any of the FCC challenges which cover using the filter method?

@jenovs It seems he wanted advice about an approach and not a solution. Try to give examples or refer to other documentation first when possible. Thanks.

Sometimes the best advice is to show the solution. Also it’s under the spoiler. You’re welcome.

I think it is always best to try and lead the users to a solution vs. giving it away. It is much more rewarding for users to figure it out on their own. For example, in this case, maybe just give a quick example of how to use filter on a similar array. That way, they can see how it works and still write their own code. Our goal here is to teach when possible. We are not meant to be Stack Overflow.

If after several back and forth replies, a user can not “get it”, then I would consider giving away a solution, but not before trying to give hints and suggestions first.

First post should contain description of the problem and what the OP has attempted to do to find a solution. If it’s written in “do my homework for me” style, I assume that the solution is what’s expected.

Also, you may have noticed that it’s under a spoiler.

All the more reason to not give away a solution.

I think you are missing my point. I will not say anything else on this thread about this.

@RandellDawson I did, some time ago. It was part of the ES6 segment, but I found the difficulty spike a bit too much when compared to previous assigments. Following advice from other campers, Ive skipped whole ES6 section and carried on. I see that maybe I should return to it and have another go.

@jenovs Although I was not expecting to receive a solution, but it certainly does help. Thank you.
But I would greatly appreciate if you could explain your code. During my research I did find something similar, when person used .every() and .include() but I assumed it wouldnt work as number of parameters to checks against would be changing in my app. You have successfully married this idea with the filter method, which seems to be very powerful and I do need to go back to it.

Thank you both :slight_smile: