gpt4 book ai didi

javascript - 如何通过javascript从没有值的json结构中删除元素?

转载 作者:行者123 更新时间:2023-11-30 13:59:27 24 4
gpt4 key购买 nike

这是我拥有的结构,我从 xml 转换为 json,但现在我不需要根元素和 element 标签:

{
"root": {
"batchRequests": [
{
"element": {
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": {},
"Status__c": "0"
}
}
},
{
"element": {
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": {},
"Status__c": "0"
}
}
}
]
}

我想把它转换成这个结构:

{
"batchRequests": [
{
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": "",
"Status__c": "0"
}
},
{
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": "",
"Status__c": "1"
}
}
]
}

如果可以从 javascript 实现,你能帮我完成这个转换吗?

最佳答案

你可以用这一行来完成。

 yourObject = { batchRequests: yourObject.root.batchRequests.map((x) => x.element) }

在这里试试。

var yourObject = {
"root": {
"batchRequests": [{
"element": {
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": {},
"Status__c": "0"
}
}
},
{
"element": {
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": {},
"Status__c": "0"
}
}
}
]
}
}

yourObject = { batchRequests: yourObject.root.batchRequests.map((x) => x.element) }

console.log(yourObject);

关于javascript - 如何通过javascript从没有值的json结构中删除元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56611975/

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