I’m trying to filter objects from a dictionary of items based on their inclusion in a list of options. For example, if I have a list of restaurants and allergens located in their kitchens, I want to remove any where shellfish and dairy are present.
I’ve been working with this code, but I honestly can’t find a good way to filter out based on the allergens:
const restaurants = [
{
"name": "Fleetwood Diner",
"allergens": [ "Eggs", "Wheat", "Dairy" ],
"Dishes": [ "Chili Cheese Fries", "Scrambled Eggs", "Omelets", "Biscuits and gravy", "Hamburgers", "Wagyu Steak"],
"image": "http://thefleetwooddiner.com/wp-content/uploads/2011/09/fleetwoodlogotopleft.jpg"
},
{
"name": "Corey's Lounge",
"allergens": [ "Dairy", "Shellfish", "Soy", "fish" ],
"Dishes": [ "Jalepeno Poppers", "Chicken Ceasar Salad", "Clam Chowder", "Center Cut Top Sirloin", "Baby Back Ribs", "Fried Lake Perch"],
"image": "https://img1.wsimg.com/isteam/ip/6687b234-bc14-4ce1-99a3-e53c1eae062b/blob-0001.png/:/rs=w:500,h:149,cg:true,m/cr=w:500,h:149/qt=q:100/ll"
},
{
"name": "Envie",
"allergens": [ "Soy", "Sesame", "Nuts" ],
"Dishes": [ "Gluten Free Olive Burger", "Tofu Stirfry", "Almond Milk Latte", "Seasonal Fruit Smoothie", "Mushrooms in Sesame Oil"],
"image": "https://envie517.com/wp-content/uploads/2017/03/18922124_1905407053058537_2614110403499260959_n.jpg?w=960&h=400&crop=1"
},
{
"name": "Subway",
"allergens": [ "Soy", "Sesame", "Nuts", "Wheat", "Eggs" ],
"Dishes": [ "Footlong sub", "6 inch sub", "cookies", "flatbread", "power bowl", "soft drinks"],
"image": "https://www.subway.com/en-us/-/media/Project/Remote-Order/Images/Logo/subway-logo.png?sc_lang=en-US"
},
{
"name": "Los Tres Amigos",
"allergens": [ "Dairy" ],
"Dishes": [ "Burritos Amigos", "Fajita Bowl", "ChiChi's Fried Icecream", "cactus burrito", "Chili Rellenos", "Special Tapatio"],
"image": "https://lostresamigosonline.com/images/los-tres-amigos-mexican-restaurant-food-catering_01.jpg"
},
]
console.log(restaurants);
function search (dictionary, terms){
for (property in terms) {
for (entry in dictionary){
var array = entry.allergens;
var newArray = array.filter( (allergen) => (allergen != property));
console.log(newArray);
}
}
return newArray;
}
var allergencheck = [“Shellfish”, “Dairy”];
search(restaurants, allergencheck).catch(console.dir);
Declann Herrington is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.