0

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?

2
  • 1
    urObject.blueprint.blueprint.product.components = urObject.blueprint.blueprint.product.components.filter(({available}) => available) Commented Aug 4, 2022 at 17:52
  • 1
    That worked, thank you, please add it as the response. Commented Aug 4, 2022 at 18:11

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.