0

I am using PrimeNG TreeTable to show hierarchy data from JSON file. I want to filter this data before binding to treetable.

Following is the sample json data.

{
"data":
[
    {
        "data":{
            "name":"Documents",
            "size":"75kb",
            "type":"Folder"
        },
        "children":[
            {
                "data":{
                    "name":"Work",
                    "size":"55kb",
                    "type":"Folder"
                },
                "children":[
                    {
                        "data":{
                            "name":"Expenses.doc",
                            "size":"30kb",
                            "type":"Document"
                        }
                    },
                    {
                        "data":{
                            "name":"Resume.doc",
                            "size":"25kb",
                            "type":"Resume"
                        }
                    }
                ]
            },
            {
                "data":{
                    "name":"Home",
                    "size":"20kb",
                    "type":"Folder"
                },
                "children":[
                    {
                        "data":{
                            "name":"Invoices",
                            "size":"20kb",
                            "type":"Text"
                        }
                    }
                ]
            }
        ]
    },
    {
        "data":{
            "name":"Pictures",
            "size":"150kb",
            "type":"Folder"
        },
        "children":[
            {
                "data":{
                    "name":"barcelona.jpg",
                    "size":"90kb",
                    "type":"Picture"
                }
            },
            {
                "data":{
                    "name":"primeui.png",
                    "size":"30kb",
                    "type":"Picture"
                }
            },
            {
                "data":{
                    "name":"optimus.jpg",
                    "size":"30kb",
                    "type":"Picture"
                }
            }
        ]
    }
]

}

I want to bind only "Home" node to treetable.

I have tried out below links but not getting result.

Filter json data in node js filter data in a JSON file

Pushkar Rathod
  • 353
  • 2
  • 7
  • 26

1 Answers1

0

You could use a filter. For example like so:

var yourData = {
   "data": [
        //...
    ]
};
var res = [];

Array.prototype.push.apply(res, yourData.data.filter(function (el) {
    return el.data.name === "Home";
});

Array.prototype.push.apply(res, yourData.data.children.filter(function (el) {
    return el.data.name === "Home";
});

You would just have to adapt the conditions to your need (depending on how deep your "Home" attribute can be). If you go say four steps deep you could probably better warp the separate filter instructions in a loop through the properties of interest like ["data", "data.children", ...] with an appropriate accessor function.

TheDude
  • 168
  • 10