使用 Deepdash 过滤复杂的 JSON 树不起作用

Filtering a complex JSON tree with Deepdash not working

const dataTree =  [
       {
          "name":"PARENT",
          "attributes":{
             "id":"6989a7d9-5150-49af-8b01-8829035ef83d",
             "layer":1,
             "parent":null,
             "category":true
          },
          "children":[
             {
                "name":"Category 1",
                "attributes":{
                   "id":"542a4cef-3bd5-47da-b7c4-962f312635c8",
                   "layer":2,
                   "parent":"6989a7d9-5150-49af-8b01-8829035ef83d",
                   "category":true
                },
                "children":[
                   {
                      "name":"Sub Category 1",
                      "attributes":{
                         "id":"a672d868-e319-4cb9-9b77-06cac3182e7c",
                         "layer":3,
                         "parent":"542a4cef-3bd5-47da-b7c4-962f312635c8",
                         "category":true
                      },
                      "children":[
                         {
                            "name":"Item 1",
                            "attributes":{
                               "category":false,
                               "toolID":"dc101f07-a12e-4290-beff-187899d8e9fb"
                            }
                         }
                      ]
                   }
                ]
             },
             {
                "name":"Category 2",
                "attributes":{
                   "id":"a5d949e5-b230-48ee-80e9-d316052442a4",
                   "layer":2,
                   "parent":"6989a7d9-5150-49af-8b01-8829035ef83d",
                   "category":true
                },
                "children":[
                   {
                      "name":"Sub Category 2",
                      "attributes":{
                         "id":"b6b11635-ba91-4469-a42a-407a1b3cece0",
                         "layer":3,
                         "parent":"a5d949e5-b230-48ee-80e9-d316052442a4",
                         "category":true
                      },
                      "children":[
                         {
                            "name":"Item 2",
                            "attributes":{
                               "category":false,
                               "toolID":"f766c7e5-e238-4293-a557-2ee8c5277e41"
                            }
                         },
                         {
                            "name":"Item 3",
                            "attributes":{
                               "category":false,
                               "toolID":"a0fd5564-9c6e-4aad-8821-4320df73a33f"
                            }
                         },
                         {
                            "name":"Item 4",
                            "attributes":{
                               "category":false,
                               "toolID":"969f663c-0b11-4bb0-a817-884f652efecb"
                            }
                         }
                      ]
                   },
                   {
                      "name":"Sub Category 3",
                      "attributes":{
                         "id":"042d7c9c-13bc-4e21-8d06-24dcfe7b63bf",
                         "layer":3,
                         "parent":"a5d949e5-b230-48ee-80e9-d316052442a4",
                         "category":true
                      },
                      "children":[
                         {
                            "name":"Item 4",
                            "attributes":{
                               "category":false,
                               "toolID":"d3a3766d-acfa-448d-b1f7-d746e9e97ed0"
                            }
                         },
                         {
                            "name":"Item 5",
                            "attributes":{
                               "category":false,
                               "toolID":"3919e2b9-aa36-47b5-9766-ba15f486dd1c"
                            }
                         },
                         {
                            "name":"Item 6",
                            "attributes":{
                               "category":false,
                               "toolID":"8944a624-10fe-4981-8612-aae0b2667fa5"
                            }
                         },
                         {
                            "name":"Sub Sub Category 1",
                            "attributes":{
                               "id":"9a787d50-9beb-43c9-ac59-56c31ea8a223",
                               "layer":4,
                               "parent":"042d7c9c-13bc-4e21-8d06-24dcfe7b63bf",
                               "category":true
                            },
                            "children":[
                               {
                                  "name":"Item 7",
                                  "attributes":{
                                     "category":false,
                                     "toolID":"12bb9de3-6af2-4e75-913e-c6890172be2d"
                                  }
                               },
                               {
                                  "name":"Item 8",
                                  "attributes":{
                                     "category":false,
                                     "toolID":"6e247fcf-35fb-4808-a1df-4acc1e889821"
                                  }
                               }
                            ]
                         }
                      ]
                   }
                ]
             },
             {
                "name":"Category 3",
                "attributes":{
                   "id":"8e9af95d-e5bf-447a-b009-62a80729ef8a",
                   "layer":2,
                   "parent":"6989a7d9-5150-49af-8b01-8829035ef83d",
                   "category":true
                },
                "children":[
                   {
                      "name":"Sub Category 5",
                      "attributes":{
                         "id":"c49860cd-924a-4d54-a823-0cc8cee5b456",
                         "layer":3,
                         "parent":"8e9af95d-e5bf-447a-b009-62a80729ef8a",
                         "category":true
                      },
                      "children":[
                         {
                            "name":"Sub Sub Category 1",
                            "attributes":{
                               "id":"54f7c21a-54b9-4e96-8bac-49e77f5b7fa2",
                               "layer":4,
                               "parent":"c49860cd-924a-4d54-a823-0cc8cee5b456",
                               "category":true
                            },
                            "children":[
                               {
                                  "name":"Item 5",
                                  "attributes":{
                                     "category":false,
                                     "toolID":"8dbe7820-fe79-49b5-992a-6eab8e775a5c"
                                  }
                               }
                            ]
                         }
                      ]
                   },
                   {
                      "name":"Sub Category 6",
                      "attributes":{
                         "id":"251a1493-5798-405a-a1fd-f356df2e0482",
                         "layer":3,
                         "parent":"8e9af95d-e5bf-447a-b009-62a80729ef8a",
                         "category":true
                      },
                      "children":[
                         
                      ]
                   }
                ]
             }
          ]
       }
    ]

我目前正在尝试过滤复杂的 JSON 树 - 以期在任何子数组为空时从数组中删除该键的父节点。在我上面的数据示例中,为清楚起见,整个:子类别 6 节点不会出现在最终数组中。

                {
                      "name":"Sub Category 6",
                      "attributes":{
                         "id":"251a1493-5798-405a-a1fd-f356df2e0482",
                         "layer":3,
                         "parent":"8e9af95d-e5bf-447a-b009-62a80729ef8a",
                         "category":true
                      },
                      "children":[
                         
                      ]
                   }

我尝试使用 Node 库 deepDash 为此:

 const filtered = _.filterDeep(dataTree, (value, key, parent) => {
    if (key === "children" && value.length === 0) return false;
    return true;
  });

这似乎是一个优雅的解决方案 - 然而,这只会删除密钥,而不是父项。想知道是否有一种简单的方法让它删除父级,或者是否有更好的方法在树上迭代和执行相同的功能。

JSON 树由具有特定结构的项目组成。嵌套项放置在项中的 "children" 键下。

因此更容易遍历项目,而不是遍历每个对象的每个键,就像它们是任意对象一样。 filterDeep 方法包含 childrenPath 选项,这应该是为了这个:

const filtered = _.filterDeep(dataTree, (value) => {
  if (value && value.children && value.children.length === 0) return false;
  return true;
}, { childrenPath: 'children' });