2017-10-16 50 views
-1

複雜的JSON我有如下一個複雜的JSON:管對角2

dashboardMenu:[ 
    { 
     category:'Physical Inventory', 
     subCategory:[ 
     { 
      'subcategory':'Upload PI Count Schedule', 
      'icon':'glyphicon-time', 
      'shortcode':'A1', 
      'router':'/physical-inventory/PIUpload' 
     }, 
     { 
      'subcategory':'Review PI Count Schedule', 
      'icon':'glyphicon-list-alt', 
      'shortcode':'B1', 
      'router':'/physical-inventory/PIReview' 
     }, 
     { 
      'subcategory':'Manage PI Group', 
      'icon':'glyphicon-wrench', 
      'shortcode':'B1', 

     } 
     ] 
    }, 
    { 
     category:'Notification Management', 
     subCategory:[ 
     { 
      'subcategory':'Create Notification', 
      'icon':'glyphicon-envelope', 
      'shortcode':'A1', 
      'router':'/notification-management/createNotification' 
     }, 
     { 
      'subcategory':'Review Notification', 
      'icon':'glyphicon-list-alt', 
      'shortcode':'B1' 
     } 
     ] 

我想補充的管道,將過濾掉類別,子類別的基礎上的數據。 我設法從類別中過濾出數據,但我無法過濾來自子類別的數據。 管 -

if (stringToSearch !== undefined) 

return items 
.filter(item => 
    item.category.toLowerCase().indexOf(stringToSearch) !== -1 || item.category.toLowerCase().indexOf(stringToSearch) !== -1 
); 

回答

0

如果你正在尋找一種方式來獲得的項目,其中包含子類別的一個嘗試:

return items.filter((item) => { 
    const filtered = item.subCategory.filter((sub) => { 
     return sub.subcategory.toLowerCase().indexOf(stringToSearch) > -1; 
    }); 
    return item.category.toLowerCase().indexOf(stringToSearch) > -1 || filtered.length; 
}); 
+0

謝謝,這是爲子類的一個工作很好,我要按類別或按子類別搜索。 – Akanksha

+0

@Akanksha所以你必須添加邏輯或返回。我編輯了我的答案給你看。 –

+0

非常感謝你的幫助。但有沒有辦法過濾出這個特定的子類別。目前,我還獲得了所有其他子分類。例如,如果我搜索上傳PI計數,我應該得到物理清單,然後只上傳PI計數子計數 – Akanksha