根据两个条件过滤嵌套的json

时间:2019-04-02 15:59:52

标签: javascript json filter

我有一个嵌套的json数组,如下所示:

[  
   {  
      "filename":"Vegetable",
      "children":[  
         {  
            "filename":"Juicy",
            "children":[  
               {  
                  "filename":"Tomato",
                  "type":"ts1"
               },
               {  
                  "filename":"Carrot",
                  "type":"ts2"
               },
               ,
               {  
                  "filename":"Onion",
                  "type":"ts3"
               }
            ]
         },
         {  
            "filename":"Sweet",
            "children":[  
               {  
                  "filename":"Potato",
                  "type":"ts4"
               },
               {  
                  "filename":"Water melon",
                  "type":"ts"
               }
            ]
         }
      ]
   },
   { filename:"Fruits"..........
   },....
]

我需要按照以下条件过滤数组:

  1. 文件名=给定的多汁(也可以是Sweet,也就是数组的第二层)
  2. 在此之下将有一个过滤器文本,假设它是't'。然后它应该匹配文件名Tomato, Carrot,因为它包含t

基于低于预期结果的条件

[  
   {  
      "filename":"Vegetable",
      "children":[  
         {  
            "filename":"Juicy",
            "children":[  
               {  
                  "filename":"Tomato",
                  "type":"ts1"
               },
               {  
                  "filename":"Carrot",
                  "type":"ts2"
               }
            ]
         },
         {  
            "filename":"Sweet",
            "children":[  
               {  
                  "filename":"Potato",
                  "type":"ts4"
               },
               {  
                  "filename":"Water melon",
                  "type":"ts"
               }
            ]
         }
      ]
   },
   { filename:"Fruits"..........
   },....
]

即应该消除

{  "filename":"Onion",
   "type":"ts3"
 }

因为洋葱不含't'。

我尝试使用箭头功能过滤器,地图,但未按预期工作。

我已经尝试过功能filterby =(childname, filterText).filter =>{...}

请指导我...

1 个答案:

答案 0 :(得分:2)

我提供了一个解决方案,该解决方案在孩子的最后一级使用两个嵌套的Array.map()Array.filter()

const input = [  
 {  
   "filename":"Vegetable",
   "children":[  
     {  
       "filename":"Juicy",
       "children":[  
         {"filename":"Tomato", "type":"ts1"},
         {"filename":"Carrot", "type":"ts2"},
         {"filename":"Onion", "type":"ts3"}
       ]
     },
     {  
       "filename":"Sweet",
       "children":[  
         {"filename":"Potato", "type":"ts4"},
         {"filename":"Water melon", "type":"ts"}
       ]
     }
   ]
 },
 {
   "filename":"Fruits",
   "children": []
 }
];

const filterBy = (arr, childname, filterText) =>
{
    return arr.map(({filename, children}) =>
    {
        return {filename, children: children.map(({filename, children}) =>
        {
           if (filename === childname)
               return {filename, children: children.filter(
                   x => x.filename.match(filterText)
               )};
           else
               return {filename, children};
        })};
    });
}

console.log(
  "Filter the Juits by text 't': ",
  filterBy(input, "Juicy", "t")
);

console.log(
  "Filter the Sweet by text 'r': ",
  filterBy(input, "Sweet", "r")
);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}