如何删除JSON Array父级并保留子json数组

时间:2018-04-27 10:28:23

标签: javascript json

我知道有很多关于删除特定JSON的问题,但当我尝试搜索任何特定问题以删除JSON数组时,我还没有找到它,所以我在这里问:

我有一个这样的json来自SQL Server Store Procedure

[
    {
        "KODE": [
            {
                "name": "kode",
                "value": [
                    {
                        "value": "DIAG001",
                        "RankNum": 1
                    },
                    {
                        "value": "DIAG002",
                        "RankNum": 1
                    },
                    {
                        "value": "DIAG003",
                        "RankNum": 1
                    },
                    {
                        "value": "DIAG004",
                        "RankNum": 1
                    }
                ]
            }
        ],
        "HEALING": [
            {
                "name": "Encountrance",
                "value": [
                    {
                        "Diagnosis_Code": "DIAG001",
                        "Name": "Subsequent"
                    },
                    {
                        "Diagnosis_Code": "DIAG002",
                        "Name": "Initial"
                    },
                    {
                        "Diagnosis_Code": "DIAG003",
                        "Name": "Initial"
                    },
                    {
                        "Diagnosis_Code": "DIAG004",
                        "Name": "Subsequent"
                    }
                ]
            }
        ]
    }
]

我想删除parent JSON array的{​​{1}},所以我的json看起来像:

KODE, HEALING

任何人都可以通过编程方式帮助我如何做到这一点?

1 个答案:

答案 0 :(得分:2)

您可以减少数组并使用对象的值进行连续。



var data = [{ KODE: [{ name: "kode", value: [{ value: "DIAG001", RankNum: 1 }, { value: "DIAG002", RankNum: 1 }, { value: "DIAG003", RankNum: 1 }, { value: "DIAG004", RankNum: 1 }] }], HEALING: [{ name: "Encountrance", value: [{ Diagnosis_Code: "DIAG001", Name: "Subsequent" }, { Diagnosis_Code: "DIAG002", Name: "Initial" }, { Diagnosis_Code: "DIAG003", Name: "Initial" }, { Diagnosis_Code: "DIAG004", Name: "Subsequent" }] }] }],
    result = data.reduce((r, o) => r.concat(...Object.values(o)), []);
    
console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }