Javascript-多个嵌套过滤器表达式

Javascript-多个嵌套过滤器表达式,javascript,arrays,Javascript,Arrays,在JavaScript中使用来自API的JSON对象: [ { "id": 1, "label": "Breakfast", "subCategories": [ { "id": 100, "label": "Cereals, Muesli", "items": [ { "productId": "4fdddf1d-8d31-411d-a908-5edd68a775

在JavaScript中使用来自API的JSON对象:

[
  {
    "id": 1,
    "label": "Breakfast",
    "subCategories": [
      {
        "id": 100,
        "label": "Cereals, Muesli",
        "items": [
          {
            "productId": "4fdddf1d-8d31-411d-a908-5edd68a775b7",
            "label": "Bircher Muesli"
          },
          {
            "productId": "000673e7-47ec-4dce-a940-ad4aacbd7d73",
            "label": "Individual Cereals"
          },
          {
            "productId": "0f739661-5531-4734-9dfd-e145b60667cc",
            "label": "Organic Porridge Oats"
          }
        ]
      },
      {
        "id": 101,
        "label": "Eggs, Omelettes",
        "items": [
          {
            "productId": "6d608133-ab44-4f9d-ab8e-fc6a3f955397",
            "label": "Crushed Avocado with Soughdough Toast"
          },
          {
            "productId": "fcfe91ab-e9b1-4dc0-8c57-ffb9646e0658",
            "label": "Crushed Avocado with Crispy Bacon"
          },
          {
            "productId": "2a80e48b-76f6-4bda-abf3-ec8dc7bf1419",
            "label": "Crushed Avocado with Smoked Salmon"
          },
          {
            "productId": "ae35e949-abf3-4795-a5df-9af4250c2185",
            "label": "Egg White Omelette"
          }
        ]
      }
      ]
  },
  {
    "id": 2,
    "label": "Light Lunch",
    "subCategories": [
      {
        "id": 103,
        "label": "Condiments",
        "items": [
          {
            "productId": "25503a9b-b553-4b56-a152-49e4121cf4ae",
            "label": "Butter"
          },
          {
            "productId": "c1dd9761-f170-4e6a-a7d7-5519a4213874",
            "label": "Jam"
          }
        ]
      },
      {
        "id": 104,
        "label": "Yoghurts",
        "items": [
          {
            "productId": "938fed24-6d4c-e0cd-8303-0fcd42c87be4",
            "label": "Fruit Yoghurt",
          },
          {
            "productId": "62137176-0966-4424-9093-51bd7871d31b",
            "label": "Greek Yoghurt",
          },
          {
            "productId": "307e59c4-b103-43d4-988c-75ee539d5d75",
            "label": "Granola Parfait: Layers of Berries, Fruit Granola, Yoghurt & Honey",
          }
        ]
      }
    ]
  }
]
我需要使用搜索查询(例如:希腊文)根据items.label属性过滤上面的数组,并让它返回过滤结果,如下所示:

[
  {
    "id": 2,
    "label": "Light Lunch",
    "subCategories": [
      {
        "id": 104,
        "label": "Yoghurts",
        "items": [
          {
            "productId": "62137176-0966-4424-9093-51bd7871d31b",
            "label": "Greek Yoghurt",
          }
        ]
      }
    ]
  }
]
我已经尝试过使用filter()和嵌套some()的各种实现,如StackOverflow上所示,但没有返回所需的结果。目前,这可以工作,但只有顶级类别被过滤,并且嵌套的子类别只有在与项匹配时才存在

var searchQuery="Greek";
var data=[]; //JSON omitted for brevity.
var result = data.filter(a=>{
    return a.subCategories.some(b=> {
        return b.items.some(c=> new RegExp(searchQuery,"i").test(c.label));
    });
});
任何帮助都将不胜感激。

您可以使用它,首先迭代每个类别,然后迭代每个子类别,如果其中一个项目包含搜索查询,则仅将子类别添加到输出,如果其中一个子类别包含搜索查询,则仅将类别添加到输出:

const数据=[{
“id”:1,
“标签”:“早餐”,
“子类别”:[{
“id”:100,
“标签”:“麦片,麦片粥”,
“项目”:[{
“产品ID”:“4fdddf1d-8d31-411d-a908-5edd68a775b7”,
“标签”:“Bircher Muesli”
},
{
“产品ID”:“000673e7-47ec-4dce-a940-ad4aacbd7d73”,
“标签”:“个别谷物”
},
{
“产品ID”:“0f739661-5531-4734-9dfd-e145b60667cc”,
“标签”:“有机燕麦粥”
}
]
},
{
“id”:101,
“标签”:“鸡蛋、煎蛋卷”,
“项目”:[{
“产品ID”:“6d608133-ab44-4f9d-ab8e-fc6a3f955397”,
“标签”:“碎鳄梨配生面团吐司”
},
{
“产品ID”:“fcfe91ab-e9b1-4dc0-8c57-ffb9646e0658”,
“标签”:“碎鳄梨配脆培根”
},
{
“产品ID”:“2a80e48b-76f6-4bda-abf3-ec8dc7bf1419”,
“标签”:“烟熏鲑鱼鳄梨碎”
},
{
“产品ID”:“ae35e949-abf3-4795-a5df-9af4250c2185”,
“标签”:“蛋清煎蛋”
}
]
}
]
},
{
“id”:2,
“标签”:“清淡午餐”,
“子类别”:[{
“id”:103,
“标签”:“调味品”,
“项目”:[{
“产品ID”:“25503a9b-b553-4b56-a152-49e4121cf4ae”,
“标签”:“黄油”
},
{
“产品ID”:“c1dd9761-f170-4e6a-a7d7-5519a4213874”,
“标签”:“果酱”
}
]
},
{
“id”:104,
“标签”:“酸奶”,
“项目”:[{
“产品ID”:“938fed24-6d4c-e0cd-8303-0fcd42c87be4”,
“标签”:“水果酸奶”,
},
{
“产品ID”:“62137176-0966-4424-9093-51bd7871d31b”,
“标签”:“希腊酸奶”,
},
{
“产品ID”:“307e59c4-b103-43d4-988c-75ee539d5d75”,
“标签”:“麦片冻糕:多层浆果、水果麦片、酸奶和蜂蜜”,
}
]
}
]
}
];
const searchQuery=“希腊语”;
const regex=new RegExp(searchQuery,“i”);
常量结果=数据。减少((猫,猫)=>{
类别子类别=类别子类别减少((子类别,子类别)=>{
sub.items=sub.items.filter(item=>regex.test(item.label));
if(子项长度)子项推送(子项);
返回潜艇;
}, []);
if(类别子类别长度)cat.push(类别);
返回猫;
}, []);
控制台日志(结果)您可以使用它,首先迭代每个类别,然后迭代每个子类别,如果其中一个项目包含搜索查询,则仅将子类别添加到输出,如果其中一个子类别包含搜索查询,则仅将类别添加到输出:

const数据=[{
“id”:1,
“标签”:“早餐”,
“子类别”:[{
“id”:100,
“标签”:“麦片,麦片粥”,
“项目”:[{
“产品ID”:“4fdddf1d-8d31-411d-a908-5edd68a775b7”,
“标签”:“Bircher Muesli”
},
{
“产品ID”:“000673e7-47ec-4dce-a940-ad4aacbd7d73”,
“标签”:“个别谷物”
},
{
“产品ID”:“0f739661-5531-4734-9dfd-e145b60667cc”,
“标签”:“有机燕麦粥”
}
]
},
{
“id”:101,
“标签”:“鸡蛋、煎蛋卷”,
“项目”:[{
“产品ID”:“6d608133-ab44-4f9d-ab8e-fc6a3f955397”,
“标签”:“碎鳄梨配生面团吐司”
},
{
“产品ID”:“fcfe91ab-e9b1-4dc0-8c57-ffb9646e0658”,
“标签”:“碎鳄梨配脆培根”
},
{
“产品ID”:“2a80e48b-76f6-4bda-abf3-ec8dc7bf1419”,
“标签”:“烟熏鲑鱼鳄梨碎”
},
{
“产品ID”:“ae35e949-abf3-4795-a5df-9af4250c2185”,
“标签”:“蛋清煎蛋”
}
]
}
]
},
{
“id”:2,
“标签”:“清淡午餐”,
“子类别”:[{
“id”:103,
“标签”:“调味品”,
“项目”:[{
“产品ID”:“25503a9b-b553-4b56-a152-49e4121cf4ae”,
“标签”:“黄油”
},
{
“产品ID”:“c1dd9761-f170-4e6a-a7d7-5519a4213874”,
“标签”:“果酱”
}
]
},
{
“id”:104,
“标签”:“酸奶”,
“项目”:[{
“产品ID”:“938fed24-6d4c-e0cd-8303-0fcd42c87be4”,
“标签”:“水果酸奶”,
},
{
“产品ID”:“62137176-0966-4424-9093-51bd7871d31b”,
“标签”:“希腊酸奶”,
},
{
“产品ID”:“307e59c4-b103-43d4-988c-75ee539d5d75”,
“标签”:“麦片冻糕:多层浆果、水果麦片、酸奶和蜂蜜”,
}
]
}
]
}
];
const searchQuery=“希腊语”;
const regex=new RegExp(searchQuery,“i
function finder(data, query) {
  for(let i in data) {
    // return the item if the label contains the search query
    if(new RegExp(query,"i").test(data[i].label)) return data[i]

    // go deeper in subCategories if exist
    if(data[i].subCategories) {
      let sub = finder(data[i].subCategories, query)
      if(sub) {
        data[i].subCategories = [sub]
        return data[i]
      }

    // go deeper in items if exist
    } else if(data[i].items){
      let item = finder(data[i].items, query)
      if(item) {
        data[i].items = [item]
        return data[i]
      }
    }
  }
  // didn't find the search query in this branch
  return false
}

console.log(finder(data, 'Greek'))