简体   繁体   English

使用地图对动态JSON键进行分组,并尽可能减少javascript

[英]Grouping dynamic JSON Keys using map and reduce if possible javascript

I want to group Dynamic Object keys of a json. 我想对JSON的动态对象键进行分组。 The object values from each keys will be group to their respective key. 每个键的对象值将被分组到各自的键。

I tried using map and reduce to group it but the results are not grouped as I expected. 我尝试使用map并将reduce进行分组,但结果未按预期分组。

Here is my JSON Object 这是我的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
  }
]

This is the code that I have tried but didn't get the result I want 这是我尝试过但未获得所需结果的代码

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

The result I got: 我得到的结果是:

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

The result I expect: 我期望的结果:

[
 {
   "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"
          }
       }
    ]
 }
]

You could reduce the array. 您可以reduce阵列。 Create an accumulator object with unique ErrorString as key. 创建一个具有唯一ErrorString作为键的累加器对象。 Set the value to be an object with the same ErrorString as key. 将值设置为具有与键相同的ErrorString的对象。 Then add each info object based on the ErrorString . 然后根据ErrorString添加每个info对象。 Use Object.values() get the grouped values as an array 使用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) 

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

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