gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:06:38 25 4
gpt4 key购买 nike

我知道关于删除特定的 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 arrayKODE, HEALING ,所以我的 json 看起来像:

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

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

最佳答案

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

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; }

关于javascript - 如何删除 JSON 数组父级并保留子级 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50060441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com