[英]Filter data from Object Array using two conditions
我有一個 object 陣列。 在每個 object 中包含屬性類別名稱和 categoryId 以及子類別列表,它是數組。我想根據名稱和子類別列表值過濾數據。這是我的示例數組
var statesWithFlags = [
{ subCategoriesList: ['Badges and dentures', 'Cerezens', 'Dental implants'], categoryId: 1, categoryName: "Dental" },
{ subCategoriesList: ['Badges and dentures44', 'Dental implants'], categoryId: 2, categoryName: "Dermatology" },
{ subCategoriesList: ['Badges and dentures', 'Cerezens', 'Dental implants222'], categoryId: 3, categoryName: "Eye" },
{ subCategoriesList: ['Badges and dentures', 'Cerezens', 'Dental implants', 'Cerezens'], categoryId: 4, categoryName: "Ayurvedic" }
]
目前,我正在使用類似的東西。這僅適用於 categoryName 屬性。 我也希望對類別列表進行修改。
this.statesWithFlags.filter(v => v.categoryName.toLowerCase().indexOf(term.toLowerCase()) > -1)).slice(0, 10))
檢查.some
subCategoriesList
是否有您要搜索的字符串,或者categoryName
是否有:
const termLower = term.toLowerCase();
this.statesWithFlags.filter(v => (
v.subCategoriesList.some(subCat => subCat.toLowerCase().includes(termLower))
|| v.categoryName.toLowerCase().includes(termLower)
))
.slice(0, 10);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.