Angular 2中复杂json的管道

时间:2017-10-16 10:37:55

标签: angular

我有一个复杂的json如下:

 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'
         }
      ]

我想添加一个管道,它将根据Category,subcategory过滤掉数据。 我已设法过滤掉类别中的数据,但我无法过滤子类别中的数据。 管道 -

if (stringToSearch !== undefined)

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

1 个答案:

答案 0 :(得分: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;
});