Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Invert nested array/objects structure and rekeying by lowest object?

I have lodash 4.17 available

I have the following API response structure:

{
    "things": [
        {
            "id": 14500,
            "name": "Q1 Out Ind",
            "thing_type_id": 6,
            "owner_id": 1680,
            "owner": {
                "id": 1680,
                "model_id": 602
            },
            "thing_type": {
                "id": 6,
                "name": "The Type of Thing"
            },
            "related_things": [
                {
                    "id": 9749,
                    "name": "unnamed thing",
                    "thing_id": 14500,
                    "more_things": [
                        {
                            "id": 16166,
                            "name": "Num Thing Sum",
                            "datasource_object_id": 9408,
                            "thing_id": 9749,
                            "external_datasource": {
                                "id": 9408,
                                "parent_id": 2810,
                                "target_id": 15028
                            }
                        }
                    ]
                }
            ]
        },
        {
            "id": 14503,
            "name": "Q2 Out Ind",
            "thing_type_id": 6,
            "owner_id": 1681,
            "owner": {
                "id": 1681,
                "model_id": 602
            },
            "thing_type": {
                "id": 6,
                "name": "The Type of Thing"
            },
            "related_things": [
                {
                    "id": 9750,
                    "name": "unnamed thing2",
                    "thing_id": 14503,
                    "more_things": [
                        {
                            "id": 16167,
                            "name": "Num Thing Avg",
                            "datasource_object_id": 9409,
                            "thing_id": 9750,
                            "external_datasource": {
                                "id": 9409,
                                "parent_id": 2810,
                                "target_id": 15029
                            }
                        },
                        {
                            "id": 16168,
                            "name": "Num Thing Count",
                            "datasource_object_id": 9408,
                            "thing_id": 9750,
                            "external_datasource": {
                                "id": 9408,
                                "parent_id": 2810,
                                "target_id": 15028
                            }
                        }
                    ]
                }
            ]
        }
    ]
}

And I am trying to get a list of objects matching specific target_id at the very end of the nesting.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So far, the following works only if there is one result in the array:

_.filter(things.things, 
      function (obj) {
        return obj.related_things[0].more_things[0].external_datasource.target_id == 15028;
      }
  )

However, as you see in this example, in this case it has two "things" at the end of which, there is a match as there is array for more_things and related_things – how do I adjust my lodash filter to search across any depth? I need a list of matching objects, to display various attributes in the UI related to the matched targets.

>Solution :

you can have nested _some functions inside the filter. By the way this is also possible with native array filter and some

const things = {    "things": [        {            "id": 14500,            "name": "Q1 Out Ind",            "thing_type_id": 6,            "owner_id": 1680,            "owner": {                "id": 1680,                "model_id": 602            },            "thing_type": {                "id": 6,                "name": "The Type of Thing"            },            "related_things": [                {                    "id": 9749,                    "name": "unnamed thing",                    "thing_id": 14500,                    "more_things": [                        {                            "id": 16166,                            "name": "Num Thing Sum",                            "datasource_object_id": 9408,                            "thing_id": 9749,                            "external_datasource": {                                "id": 9408,                                "parent_id": 2810,                                "target_id": 15028                            }                        }                    ]                }            ]        },        {            "id": 14503,            "name": "Q2 Out Ind",            "thing_type_id": 6,            "owner_id": 1681,            "owner": {                "id": 1681,                "model_id": 602            },            "thing_type": {                "id": 6,                "name": "The Type of Thing"            },            "related_things": [                {                    "id": 9750,                    "name": "unnamed thing2",                    "thing_id": 14503,                    "more_things": [                        {                            "id": 16167,                            "name": "Num Thing Avg",                            "datasource_object_id": 9409,                            "thing_id": 9750,                            "external_datasource": {                                "id": 9409,                                "parent_id": 2810,                                "target_id": 15029                            }                        },                        {                            "id": 16168,                            "name": "Num Thing Count",                            "datasource_object_id": 9408,                            "thing_id": 9750,                            "external_datasource": {                                "id": 9408,                                "form_id": 2810,                                "target_id": 15028                            }                        }                    ]                }            ]        }    ]}

const id = 15028

const res = _.filter(things.things, a => {
  return _.some(a.related_things, b => {
    return _.some(b.more_things, c => c.external_datasource.target_id === id)
  })
})

console.log(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

without lodash

const things = {    "things": [        {            "id": 14500,            "name": "Q1 Out Ind",            "thing_type_id": 6,            "owner_id": 1680,            "owner": {                "id": 1680,                "model_id": 602            },            "thing_type": {                "id": 6,                "name": "The Type of Thing"            },            "related_things": [                {                    "id": 9749,                    "name": "unnamed thing",                    "thing_id": 14500,                    "more_things": [                        {                            "id": 16166,                            "name": "Num Thing Sum",                            "datasource_object_id": 9408,                            "thing_id": 9749,                            "external_datasource": {                                "id": 9408,                                "parent_id": 2810,                                "target_id": 15028                            }                        }                    ]                }            ]        },        {            "id": 14503,            "name": "Q2 Out Ind",            "thing_type_id": 6,            "owner_id": 1681,            "owner": {                "id": 1681,                "model_id": 602            },            "thing_type": {                "id": 6,                "name": "The Type of Thing"            },            "related_things": [                {                    "id": 9750,                    "name": "unnamed thing2",                    "thing_id": 14503,                    "more_things": [                        {                            "id": 16167,                            "name": "Num Thing Avg",                            "datasource_object_id": 9409,                            "thing_id": 9750,                            "external_datasource": {                                "id": 9409,                                "parent_id": 2810,                                "target_id": 15029                            }                        },                        {                            "id": 16168,                            "name": "Num Thing Count",                            "datasource_object_id": 9408,                            "thing_id": 9750,                            "external_datasource": {                                "id": 9408,                                "form_id": 2810,                                "target_id": 15028                            }                        }                    ]                }            ]        }    ]}

const id = 15028

const res = things.things.filter(a => {
  return a.related_things.some(b => {
    return b.more_things.some(c => c.external_datasource.target_id === id)
  })
})

console.log(res)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading