Skip to content
Advertisement

Filter an array nested within an object

I was able to find many sources saying how to filter nested arrays, but all of these referred to arrays within arrays. In my case, there’s an array nested within an object and I can’t figure out how to deal with it.

The source array is:

{
    "msg": "OK",
    "blueprint": {
        "result": "OK",
        "blueprint": {
            "id": "a2e63ee01401aaeca78be023dfbb8c59",
            "product": {
                "productName": "Test Product",
                "productId": "AS_12-01",
                "description": "Test Descr.",
                "childProducts": [
                    {
                        "childId": "T1",
                        "parent": "8c59"
                    },
                    {
                        "childId": "T5",
                        "parent": "8c7e"
                    }
                ],
                "components": [
                    {
                        "compId": "C2",     #
                        "leadTime": 21,     # remove
                        "available": false  #
                    },
                    {
                        "compId": "C5",
                        "leadTime": 3,
                        "available": true
                    },
                    {
                        "compId": "C6",     # 
                        "leadTime": 12,     # remove
                        "available": false  # 
                    },
                    {
                        "compId": "C8",
                        "leadTime": 5,
                        "available": true
                    },
                ]
            },
            "owner": "dummy",
            "name": "du_test"
        }
    }
}

How to filter the nested array so that the resulting object looks like:

{
    "msg": "OK",
    "blueprint": {
        "result": "OK",
        "blueprint": {
            "id": "a2e63ee01401aaeca78be023dfbb8c59",
            "product": {
                "productName": "Test Product",
                "productId": "AS_12-01",
                "description": "Test Descr.",
                "childProducts": [
                    {
                        "childId": "T1",
                        "parent": "8c59"
                    },
                    {
                        "childId": "T5",
                        "parent": "8c7e"
                    }
                ],
                "components": [
                    {
                        "compId": "C5",
                        "leadTime": 3,
                        "available": true
                    },
                    {
                        "compId": "C8",
                        "leadTime": 5,
                        "available": true
                    },
                ]
            },
            "owner": "dummy",
            "name": "du_test"
        }
    }
}

So, basically the structure is the same, only the nested array has the unavailable objects removed.

How to achieve it?

Advertisement

Answer

you can simply reassign the field with the filtered array

const x = {    "msg": "OK",    "blueprint": {        "result": "OK",        "blueprint": {            "id": "a2e63ee01401aaeca78be023dfbb8c59",            "product": {                "productName": "Test Product",                "productId": "AS_12-01",                "description": "Test Descr.",                "childProducts": [                    {                        "childId": "T1",                        "parent": "8c59"                    },                    {                        "childId": "T5",                        "parent": "8c7e"                   }                ],                "components": [                    {                        "compId": "C2",                           "leadTime": 21,                             "available": false                     },                    {                        "compId": "C5",                        "leadTime": 3,                        "available": true                    },                    {                        "compId": "C6",                            "leadTime": 12,                            "available": false                     },                    {                        "compId": "C8",                        "leadTime": 5,                        "available": true                    },                ]            },            "owner": "dummy",            "name": "du_test"        }    }}

x.blueprint.blueprint.product.components = x.blueprint.blueprint.product.components.filter(({available}) => available)

console.log(x)
Advertisement