在我的角度應用程序中,我得到的響應如下。按ID分組,並從分組數據中創建一個數組
[{
"proposalId": "cc0bcd90-fc75-4c73-bd66-799b37330fb5",
"proposalGpId": "cc0bcd90-fc75-4c73-bd66-799b37380fb5",
"eventId": "17e69ea1-54da-11e7-9469-985aebe2005e",
"listingId": "pdf19f7e1-5f2b-4d02-a48d-3b6d0189b5d1",
"status": "Approved"
}, {
"proposalId": "cc0bcd90-fc75-4c73-bd66-799b373306b5",
"proposalGpId": "cc0bcd90-fc75-4c73-bd66-799b37380fb5",
"eventId": "17e69ea1-54da-11e7-9469-985aebe2005e",
"listingId": "pdf19f7e1-5f2b-4d02-a48d-3b6d0189b5d1",
"status": "Approved"
}]
我有上的應用程序爲兩個類,
ProposalGroup.ts
export class ProposalGroup {
eventId: string;
proposalGpId: string;
proposals: Proposal[];
}
Proposal.ts
export class Proposal {
proposalId: string;
listingId: Date;
eventId: Price;
status: string;
}
我需要從上面的JSON中創建一個ProposalGroups數組,何時提交具有相同的listingId應該進行分組。
我要生成一個output
如下,
[{
"eventId": "17e69ea1-54da-11e7-9469-985aebe2005e",
"proposals": [{
"proposalId": "cc0bcd90-fc75-4c73-bd66-799b37330fb5",
"proposalGpId": "cc0bcd90-fc75-4c73-bd66-799b37380fb5",
"eventId": "17e69ea1-54da-11e7-9469-985aebe2005e",
"listingId": "pdf19f7e1-5f2b-4d02-a48d-3b6d0189b5d1",
"status": "approved"
}, {
"proposalId": "cc0bcd90-fc75-4c73-bd66-799b373306b5",
"proposalGpId": "cc0bcd90-fc75-4c73-bd66-799b37380fb5",
"eventId": "17e69ea1-54da-11e7-9469-985aebe2005e",
"listingId": "pdf19f7e1-5f2b-4d02-a48d-3b6d0189b5d1",
"status": "approved"
}]
}]
這就是我一直在使用lodash嘗試,
const grouped = _.groupBy(this.proposals, (proposal: Proposal) => proposal.listingId);
但上面給出了一個解釋。
一個事件可以有多個清單的建議?因爲在這種情況下,你不能將一切都放在一個數組中。或者你在談論嵌套數組,即分組提議數組的數組? – andreiho