在我的角度应用程序中,我得到的响应如下。
[{
"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);
但上面给出了一本字典。
答案 0 :(得分:2)
var 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"
}];
console.log('proposals : ', _.chain(/*this.*/proposals)
.groupBy("listingId")
.transform((result, value, key)=>{
result.push({"listingId": key, proposals: value});
}, [])
.value()
);
.as-console-wrapper { max-height: 100%!important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
这会将字典转换为对象数组。
答案 1 :(得分:1)
将组分组,然后将组映射到新数组:
const 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"}];
const result = _(proposals)
.groupBy('listingId') // group by listingId
.map((proposals, listingId) => (({
eventId: proposals[0].eventId, // omit this if you don't want the eventId
listingId, // omit it if you don't want the listingId
proposals
})))
.value();
console.log(result);
.as-console-wrapper { max-height: 100%!important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>