简体   繁体   English

过滤嵌套在 object 中的数组

[英]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.我能够找到许多来源说如何过滤嵌套的 arrays,但所有这些都提到 arrays 中的 arrays。 In my case, there's an array nested within an object and I can't figure out how to deal with it.在我的例子中,有一个嵌套在 object 中的数组,我不知道如何处理它。

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:如何过滤嵌套数组,使生成的 object 看起来像:

{
    "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?如何实现?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM