javascript - Merge multiple json objects iniside an array to a Desired format -


i have json file goes below

[{     "data": {         "aggregations": {             "openissue": {                 "buckets": [{                         "doc_count": 5,                         "date": "25 aug"                     },                     {                         "doc_count": 6,                         "date": "26 aug"                     },                     {                         "doc_count": 7,                         "date": "27 aug"                     },                     {                         "doc_count": 8,                         "date": "28 aug"                     }                 ]             }         }      },     "a": "not needed",     "b": "not needed" }, {     "data": {         "aggregations": {             "closeissue": {                 "buckets": [{                         "doc_count": 9,                         "date": "29 aug"                     },                     {                         "doc_count": 10,                         "date": "30 aug"                     },                     {                         "doc_count": 11,                         "date": "31 aug"                     },                     {                         "doc_count": 12,                         "date": "1 sept"                     }                 ]             }         }      },     "a": "not needed",     "b": "not needed" }, {     "data": {         "aggregations": {             "pendingissue": {                 "buckets": [{                         "doc_count": 13,                         "date": "10 sept"                     },                     {                         "doc_count": 14,                         "date": "13 sept"                     },                     {                         "doc_count": 15,                         "date": "18 sept"                     },                     {                         "doc_count": 16,                         "date": "19 sept"                     }                 ]             }         }      },     "a": "not needed",     "b": "not needed" } 

]

the number of objects dynamic getting ajax call,this json created after ajax call.i trying filter out , json desired format number of object depends on number of date.

the desired output should

    [   {     "key_as_string": "25aug",     "values": [       {         "label": "open-issues",         "doc_count": 10       },       {         "label": "closed-issues",         "doc_count": 10       },       {         "label": "exempted-issues",         "doc_count": 10       }     ]   },   {     "key_as_string": "26aug",     "values": [       {         "label": "open-issues",         "doc_count": 10       },       {         "label": "closed-issues",         "doc_count": 10       },       {         "label": "pending-issues",         "doc_count": 10       }     ]   } ] 

how do using javascript or jquery

the idea flatten issues single array , group date, construct required format.

const payload = [  {      "data": {          "aggregations": {              "openissue": {                  "buckets": [{                          "doc_count": 5,                          "date": "25 aug"                      },                      {                          "doc_count": 6,                          "date": "26 aug"                      },                      {                          "doc_count": 7,                          "date": "27 aug"                      },                      {                          "doc_count": 8,                          "date": "28 aug"                      }                  ]              }          }        },      "a": "not needed",      "b": "not needed"  },  {      "data": {          "aggregations": {              "closeissue": {                  "buckets": [{                          "doc_count": 9,                          "date": "29 aug"                      },                      {                          "doc_count": 10,                          "date": "30 aug"                      },                      {                          "doc_count": 11,                          "date": "31 aug"                      },                      {                          "doc_count": 12,                          "date": "1 sept"                      }                  ]              }          }        },      "a": "not needed",      "b": "not needed"  }, {      "data": {          "aggregations": {              "pendingissue": {                  "buckets": [{                          "doc_count": 13,                          "date": "10 sept"                      },                      {                          "doc_count": 14,                          "date": "13 sept"                      },                      {                          "doc_count": 15,                          "date": "18 sept"                      },                      {                          "doc_count": 16,                          "date": "19 sept"                      }                  ]              }          }        },      "a": "not needed",      "b": "not needed"  }  ]  //  let flat = []  payload.foreach(item => {  	const preissuetype = object.keys(item.data.aggregations)[0]    item.data.aggregations[preissuetype].buckets.foreach(issue => {  			flat.push({label: preissuetype, doc_count: issue.doc_count, date: issue.date})  	    })  })  const grouped = _.groupby(flat, 'date')  let result = []  for(const key in grouped){  	result.push({'key_as_string': key, values: grouped[key]})  }    console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

forgot say, lodash not compulsory save coding effort.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -