改变Json数据结构

时间:2017-09-26 10:56:59

标签: javascript jquery json

我有一个从服务器端使用此结构生成的json文件。

    "reports": [
    {
    "internalId": 60,
    "state": "DIAGNOSIS_APPROVAL",
    "project": "pro1",
    "application": "app1",
    "decision": null,
    "reference": "IMUPT17_60",
    "instanceName": "Report"
    },
    {
    "internalId": 62,
    "state": "DIAGNOSIS_APPROVAL",
    "project": "pro1",
    "application": "app2",
    "decision": null,
    "reference": "IMUPT17_62",
    "instanceName": "Report"
    },
    {
    "internalId": 65,
    "state": "DIAGNOSIS_APPROVAL",
    "project": "pro1",
    "application": "app3",
    "decision": null,
    "reference": "IMUPT17_62",
    "instanceName": "Report"
    },


   {
    "internalId": 62,
    "state": "DIAGNOSIS_APPROVAL",
    "project": "pro1",
    "application": "app2",
    "decision": null,
    "reference": "IMUPT17_62",
    "instanceName": "Report"
    }

我想按项目对数据进行分组,所以我希望新的json结构就像这样

"pro1": [
    {
    "internalId": 60,
    "state": "DIAGNOSIS_APPROVAL",
    "application": "app1",
    "decision": null,
    "reference": "IMUPT17_60",
    "instanceName": "Report"
    },
    {
    "internalId": 62,
    "state": "DIAGNOSIS_APPROVAL",
    "project": "pro1",
    "application": "app2",
    "decision": null,
    "reference": "IMUPT17_62",
    "instanceName": "Report"
    },
    {
    "internalId": 62,
    "state": "DIAGNOSIS_APPROVAL",
    "project": "pro1",
    "application": "app3",
    "decision": null,
    "reference": "IMUPT17_62",
    "instanceName": "Report"
    },


   {
    "internalId": 62,
    "state": "DIAGNOSIS_APPROVAL",
    "project": "pro1",
    "application": "app2",
    "decision": null,
    "reference": "IMUPT17_62",
    "instanceName": "Report"
    }

问题是我没有访问服务器端创建一个服务,生成我想要的结构,所以我必须使用现有的服务。 当我使用_.groupBy作为例子时,它没有给出我想要的结构。 任何人都可以帮助我或提供一些框架来解决这个问题。

4 个答案:

答案 0 :(得分:0)

您已将下面提到的代码放入服务器端返回值的位置。

return“report:”。json_encode('you_array_return_value');

答案 1 :(得分:0)

您应该使用._groupBy库中的underscore.js方法,它会起作用。

let obj={"reports": [ { "internalId": 60, "state": "DIAGNOSIS_APPROVAL", "project": "pro1", "application": "app1", "decision": null, "reference": "IMUPT17_60", "instanceName": "Report" }, { "internalId": 62, "state": "DIAGNOSIS_APPROVAL", "project": "pro1", "application": "app2", "decision": null, "reference": "IMUPT17_62", "instanceName": "Report" }, { "internalId": 65, "state": "DIAGNOSIS_APPROVAL", "project": "pro1", "application": "app3", "decision": null, "reference": "IMUPT17_62", "instanceName": "Report" },{ "internalId": 62, "state": "DIAGNOSIS_APPROVAL", "project": "pro1", "application": "app2", "decision": null, "reference": "IMUPT17_62", "instanceName": "Report" }]}
var groups = _.groupBy(obj.reports, 'project');
console.log(groups)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore.js"></script>

答案 2 :(得分:0)

你可以使用像ramdajs这样的库轻松实现这个目标

const data = {
  "reports": [{
      "internalId": 60,
      "state": "DIAGNOSIS_APPROVAL",
      "project": "pro1",
      "application": "app1",
      "decision": null,
      "reference": "IMUPT17_60",
      "instanceName": "Report"
    },
    {
      "internalId": 62,
      "state": "DIAGNOSIS_APPROVAL",
      "project": "pro1",
      "application": "app2",
      "decision": null,
      "reference": "IMUPT17_62",
      "instanceName": "Report"
    },
    {
      "internalId": 65,
      "state": "DIAGNOSIS_APPROVAL",
      "project": "pro1",
      "application": "app3",
      "decision": null,
      "reference": "IMUPT17_62",
      "instanceName": "Report"
    },
    {
      "internalId": 62,
      "state": "DIAGNOSIS_APPROVAL",
      "project": "pro1",
      "application": "app2",
      "decision": null,
      "reference": "IMUPT17_62",
      "instanceName": "Report"
    }
  ]
};



const byProp = R.groupBy(R.prop('project'));

const output = byProp(data.reports);
console.log(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.24.1/ramda.min.js"></script>

答案 3 :(得分:0)

var reports = [] // this is the array you get back from server
var result = {} // this will be the result of the grouping

// iterate over the result
reports.forEach(function(report) {
    // make sure you make a new object for every project
    if (!result[report.project]){
        result[report.project] = []
    }

    // add the line from the result to the result array under the project
    result[report.project].push(report)
})

这不应该为这种事情添加框架后应该做的就是