Search code examples
javascriptarraysjsonlodash

Filter a json dataset by a field 4 levels deep


Take the following json structure, how can I filter it on the categories node that is 4 levels down.

For example, only return a json object which contains items that have "cat8" in this example, i'd get Items A and B?

I'm using node and have lodash installed.

I would filter the data at source normally, but the dataset I have, I can't do this.

{
    "items": [
        {
            "fields": {
                "title": "Item A",
                "section1": {
                    "fields": {
                        "title": "Level 2 title",
                        "section2": {
                            "fields": {
                                "title": "Level 3 title",
                                "section3": {
                                    "fields": {
                                        "title": "Level 4 title",
                                        "categories": [
                                            "cat1",
                                            "cat6",
                                            "cat8"
                                        ]
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        {
            "fields": {
                "title": "Item B",
                "section1": {
                    "fields": {
                        "title": "Level 2 title",
                        "section2": {
                            "fields": {
                                "title": "Level 3 title",
                                "section3": {
                                    "fields": {
                                        "title": "Level 4 title",
                                        "categories": [
                                            "cat1",
                                            "cat8"
                                        ]
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        {
            "fields": {
                "title": "Item C",
                "section1": {
                    "fields": {
                        "title": "Level 2 title",
                        "section2": {
                            "fields": {
                                "title": "Level 3 title",
                                "section3": {
                                    "fields": {
                                        "title": "Level 4 title",
                                        "categories": [
                                            "cat1",
                                            "cat3"
                                        ]
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    ]
}

Solution

  • with lodash its pretty simple

    const results = _.filter(deepObject,['path.to.deep.property', expectedValue])