2017-10-28 167 views
-1

在我的角度應用程序中,我得到的響應如下。按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); 

但上面給出了一個解釋。

+0

一個事件可以有多個清單的建議?因爲在這種情況下,你不能將一切都放在一個數組中。或者你在談論嵌套數組,即分組提議數組的數組? – andreiho

回答

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>

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>

這將轉化成字典對象數組。

+0

'錯誤' 消息:'屬性'轉換'不存在於類型'LoDashExplicitObjectWrapper >'。'。 –

+0

嘗試@Peter Selinger更新的片段,我的回答是基於最新的lodash文檔。 – IOR88