gpt4 book ai didi

javascript - 使用 map 对动态 JSON 键进行分组,并在可能的情况下减少 javascript

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

我想对 json 的动态对象键进行分组。来自每个键的对象值将被分组到它们各自的键中。

我尝试使用 map 和 reduce 对其进行分组,但结果并没有像我预期的那样分组。

这是我的 JSON 对象

var data = [
{
"type": 6,
"data": {
"error": {
"cause": {
"root": {
"Extracted": {
"Body": {
"Error": {
"ErrorCode": "143",
"ErrorString": "NotFound",
"info": {
"Error": {
"errorDesc": "Data Not Found",
"subs": {
"attrib": {
"subs_name": "123com",
"subs_no": 4
}
}
}
}
}
}
}
}
}
}
},
"status": true
},
{
"type": 6,
"data": {
"error": {
"cause": {
"root": {
"Extracted": {
"Body": {
"Error": {
"ErrorCode": "143",
"ErrorString": "NotFound",
"info": {
"Error": {
"errorDesc": "Company Not Found",
"subs": {
"attrib": {
"subs_name": "QRS",
"subs_no": 4
}
}
}
}
}
}
}
}
}
}
},
"status": true
},
{
"type": 6,
"data": {
"error": {
"cause": {
"root": {
"Extracted": {
"Body": {
"Error": {
"ErrorCode": "123",
"ErrorString": "SystemFailure",
"info": {
"Error": {
"errorDesc": "Internal server error",
"subs": {
"attrib": {
"subs_name": "ABC",
"subs_no": 2
}
}
}
}
}
}
}
}
}
}
},
"status": true
},
{
"type": 6,
"data": {
"error": {
"cause": {
"root": {
"Extracted": {
"Body": {
"Error": {
"ErrorCode": "123",
"ErrorString": "SystemFailure",
"info": {
"Error": {
"errorDesc": "Insufficient Data",
"subs": {
"attrib": {
"subs_name": "DEF",
"subs_no": 3
}
}
}
}
}
}
}
}
}
}
},
"status": true
},
{
"type": 6,
"data": {
"error": {
"cause": {
"root": {
"Extracted": {
"Body": {
"Error": {
"ErrorCode": "999",
"ErrorString": "Unknown",
"info": {
"Unknown": {
"desc": "UnknownError",
"subs": "GHI"
}
}
}
}
}
}
}
}
}
},
"status": true
}
]

这是我试过但没有得到我想要的结果的代码

var x = data.map((e) => {
var el = {}
el[e.data.error.cause.root.Extracted.Body.Error.ErrorString] =
[e.data.error.cause.root.Extracted.Body.Error.info];

return el;
})

console.log(x);

我得到的结果:

 [
{
"NotFound": {....}
},
{
"NotFound": {....}
},
{
"SystemFailure": {....}
},
{
"SystemFailure": {....}
},
{
"Unknown": {....}
},
]

我期望的结果:

[
{
"NotFound": [
{
"Error": {
"errorDesc": "Data Not Found",
"subs": {
"attrib": {
"subs_name": "123com",
"subs_no": 4
}
}
},
{
"Error": {
"errorDesc": "Company Not Found",
"subs": {
"attrib": {
"subs_name": "QRS",
"subs_no": 4
}
}
}
}
]
},
{
"SystemFailure": [
{
"Error": {
"errorDesc": "Internal server error",
"subs": {
"attrib": {
"subs_name": "ABC",
"subs_no": 2
}
}
},
{
"Error": {
"errorDesc": "Insufficient Data",
"subs": {
"attrib": {
"subs_name": "DEF",
"subs_no": 3
}
}
}
]
},
{
"Unknown": [
{
"Unknown": {
"desc": "UnknownError",
"subs": "GHI"
}
}
]
}
]

最佳答案

您可以减少 数组。使用唯一的 ErrorString 作为键创建一个累加器对象。将值设置为与键具有相同 ErrorString 的对象。然后根据 ErrorString 添加每个 info 对象。使用 Object.values()将分组的值作为数组获取

const data=[{type:6,data:{error:{cause:{root:{Extracted:{Body:{Error:{ErrorCode:"143",ErrorString:"NotFound",info:{Error:{errorDesc:"Data Not Found",subs:{attrib:{subs_name:"123com",subs_no:4}}}}}}}}}}},status:true},{type:6,data:{error:{cause:{root:{Extracted:{Body:{Error:{ErrorCode:"143",ErrorString:"NotFound",info:{Error:{errorDesc:"Company Not Found",subs:{attrib:{subs_name:"QRS",subs_no:4}}}}}}}}}}},status:true},{type:6,data:{error:{cause:{root:{Extracted:{Body:{Error:{ErrorCode:"123",ErrorString:"SystemFailure",info:{Error:{errorDesc:"Internal server error",subs:{attrib:{subs_name:"ABC",subs_no:2}}}}}}}}}}},status:true},{type:6,data:{error:{cause:{root:{Extracted:{Body:{Error:{ErrorCode:"123",ErrorString:"SystemFailure",info:{Error:{errorDesc:"Insufficient Data",subs:{attrib:{subs_name:"DEF",subs_no:3}}}}}}}}}}},status:true},{type:6,data:{error:{cause:{root:{Extracted:{Body:{Error:{ErrorCode:"999",ErrorString:"Unknown",info:{Unknown:{desc:"UnknownError",subs:"GHI"}}}}}}}}},status:true}];

const merged = data.reduce((acc, o) => {
const e = o.data.error.cause.root.Extracted.Body.Error;
acc[e.ErrorString] = acc[e.ErrorString] || { [e.ErrorString]: [] };
acc[e.ErrorString][e.ErrorString].push(e.info)
return acc;
}, {})

const output = Object.values(merged);

console.log(output)

关于javascript - 使用 map 对动态 JSON 键进行分组,并在可能的情况下减少 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57479901/

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