将数组中的多个json对象合并为Desired格式

时间:2017-09-14 01:23:13

标签: javascript jquery json

我有一个json文件,如下所示

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

对象的数量是动态的,我通过ajax调用获得,这个json是在ajax调用之后创建的。我试图过滤掉json到所需的格式,其中对象的数量取决于日期的数量

Desired输出应该类似于

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

我如何使用javascript或jquery

1 个答案:

答案 0 :(得分:0)

我们的想法是将所有问题展平为一个数组,然后按日期分组,最后将其构建为所需的格式。



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>
&#13;
&#13;
&#13;

忘了说,lodash不是强制性的,但会为你节省一些编码工作。