'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":[
]
}
]
}
]
}
]
I'm currently trying to filter a complex JSON tree - with a view that when any children arrays are empty remove the parent node of that key from the array. In my data sample above, for clarity, the entire: Sub Category 6 node would not be present in the final array.
{
"name":"Sub Category 6",
"attributes":{
"id":"251a1493-5798-405a-a1fd-f356df2e0482",
"layer":3,
"parent":"8e9af95d-e5bf-447a-b009-62a80729ef8a",
"category":true
},
"children":[
]
}
I tried using the Node library deepDash for this:
const filtered = _.filterDeep(dataTree, (value, key, parent) => {
if (key === "children" && value.length === 0) return false;
return true;
});
Which seems like it would be an elegant solution - however, this only removes the key, and not the parent. Wondering if there's an easy way to get it to remove the parent, or alternatively a better way to iterate and perform the same function on the tree.
Solution 1:[1]
The JSON tree consists of items with a certain structure. The nested items are placed under the "children" key in an item.
So it's easier to iterate over the items, not over each key of each object like they are arbitrary objects.
The filterDeep method contains childrenPath option, which is supposed exactly for this:
const filtered = _.filterDeep(dataTree, (value) => {
if (value && value.children && value.children.length === 0) return false;
return true;
}, { childrenPath: 'children' });
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | yarikos |
