Saya ingin mengelompokkan kunci Objek Dinamis dari json. Nilai objek dari setiap kunci akan dikelompokkan ke kunci masing-masing.

Saya mencoba menggunakan peta dan mengurangi untuk mengelompokkannya tetapi hasilnya tidak dikelompokkan seperti yang saya harapkan.

Ini Objek JSON saya

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

Ini adalah kode yang saya coba tetapi tidak mendapatkan hasil yang saya inginkan

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

Hasil yang saya dapatkan:

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

Hasil yang saya harapkan:

[
 {
   "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"
          }
       }
    ]
 }
]
1
vashlor 13 Agustus 2019, 17:28

1 menjawab

Jawaban Terbaik

Anda bisa reduce array. Buat objek akumulator dengan ErrorString unik sebagai kunci. Tetapkan nilainya menjadi objek dengan ErrorString yang sama sebagai kunci. Kemudian tambahkan setiap objek info berdasarkan ErrorString. Gunakan Object.values() dapatkan nilai yang dikelompokkan sebagai array

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)
2
adiga 13 Agustus 2019, 14:48