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

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

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.

@camperextraordinaire 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: