Я пытаюсь отфильтровать каждое учреждение в массиве establishments
по тому, найден ли specialCategories
в массиве establishmentSpecials
самого массива establishments
.
Я могу отфильтровать для массива establishmentSpecials
для каждого заведения, но я хочу сохранить часть заведения, как видно на рисунке.
//masterEstablishments array is from the db
//The categories array, to filter out all but the following
specialCategories = ["Breakfast Special"];
//The array to hold the filtered establishments
var establishments;
//For all Establishments
for(var i=0;i<masterEstablishments.length;i++){
//Filter out Catorgory from establishmentSpecials where Category is not found in specialCategories
establishments = masterEstablishments[i].establishmentSpecials.filter(x => specialCategories.includes(x.Category));
}
console.log("Filtered establishments");
console.log(establishments);
В итоге, например:
var establishments = [ {address: "55 Garden rd"},
[{Category: "Breakfast Special"}, {Category: "Dinner Special"}]
];
Должен стать
establishments = [ {address: "55 Garden rd"},
[{Category: "Breakfast Special"}]
];
, поскольку «Специальное ужиновое» не входит в специальныеКатегории
specialCategory = [{Category: "Breakfast Special"}]
Большое спасибо.