通过值列表过滤字典中的数组

问题描述 投票:0回答:1

我正在尝试根据选项列表中包含的项目从项目字典中过滤对象。例如,如果我有一份餐馆及其厨房中过敏原的清单,我想删除所有存在贝类和乳制品的地方。

我一直在使用这段代码,但老实说我找不到根据过敏原进行过滤的好方法: 常吃餐馆= [

       {

         "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(餐厅);

功能搜索(词典、术语){

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 = ["贝类", "乳制品"];

search(餐厅、过敏原检查).catch(console.dir);

javascript arrays filter
1个回答
0
投票

for..in
循环将循环遍历对象的属性,对于数组来说,就是它的索引。因此,您的代码当前存在缺陷,因为它试图将过敏(字符串)与索引进行比较。

你想要的是对你的

.filter()
数组执行
restaurants
,然后保留餐厅(即:从过滤器回调返回
true
),如果所有(即:
.every()
allergens
都不是包含在您的
terms
数组中(可以使用
.includes()
检查):

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"

       },

     ];

function search (arr, terms){
  const filteredRestaurants = arr.filter(obj => obj.allergens.every(
    allergen => !terms.includes(allergen)
  ));
  return filteredRestaurants;
}

const allergencheck = ["Shellfish", "Dairy"];

const res = search(restaurants, allergencheck);
console.log(res);

© www.soinside.com 2019 - 2024. All rights reserved.