2017-09-26 173 views
0

我有一個從服務器端生成這個結構的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 stucture會是這樣

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

的問題是,我沒有訪問服務器端的創建服務產生我想要的結構,所以我必須使用existant服務。 當我使用_.groupBy作爲例子,它不給我想要的結構。 任何人都可以幫助我或給一些框架來解決這個問題。

回答

0

你已經把下面提到的代碼放到你的服務器端返回值的地方。

return「report:」。json_encode('you_array_return_value');

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>

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>

+0

什麼是「R」在代碼 –

+0

它來自ramdajs庫http://ramdajs.com/。用於訪問該庫函數 – user93

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

本應該做的事情,你是後無需添加框架r這種東西