Предполагая, что у меня есть этот набор данных:
"results": [
{
"__metadata": {
"uri": "https://xxxx.sapbydesign.com/sap/byd/odata/ana_businessanalytics_analytics.svc/RPHCMRPMU01_Q1001QueryResults('%7CCCALENDAR_DAY%3D02%2F19%2F2020%7CCCALENDAR_MONTH%3D02%2F2020%7CCCALENDAR_WEEK%3D08%2F2020%7CCEMPLOYEE%3DEXT011%7C')",
"type": "sapbyd.RPHCMRPMU01_Q1001QueryResult"
},
"CCALENDAR_DAY": "/Date(1582070400000)/",
"CCALENDAR_MONTH": "02/2020",
"CCALENDAR_WEEK": "08/2020",
"CEMPLOYEE": "EXT011",
"KCGROSS_CAPACITY": "8.00000000000000",
"KCVACATION": "0.00000000000000",
"UCGROSS_CAPACITY": "HUR",
"UCVACATION": "HUR"
},
{
"__metadata": {
"uri": "https://xxxx.sapbydesign.com/sap/byd/odata/ana_businessanalytics_analytics.svc/RPHCMRPMU01_Q1001QueryResults('%7CCCALENDAR_DAY%3D02%2F20%2F2020%7CCCALENDAR_MONTH%3D02%2F2020%7CCCALENDAR_WEEK%3D08%2F2020%7CCEMPLOYEE%3DEXT011%7C')",
"type": "sapbyd.RPHCMRPMU01_Q1001QueryResult"
},
"CCALENDAR_DAY": "/Date(1582156800000)/",
"CCALENDAR_MONTH": "02/2020",
"CCALENDAR_WEEK": "08/2020",
"CEMPLOYEE": "EXT011",
"KCGROSS_CAPACITY": "8.00000000000000",
"KCVACATION": "0.00000000000000",
"UCGROSS_CAPACITY": "HUR",
"UCVACATION": "HUR"
},
{
"__metadata": {
"uri": "https://xxxx.sapbydesign.com/sap/byd/odata/ana_businessanalytics_analytics.svc/RPHCMRPMU01_Q1001QueryResults('%7CCCALENDAR_DAY%3D02%2F21%2F2020%7CCCALENDAR_MONTH%3D02%2F2020%7CCCALENDAR_WEEK%3D08%2F2020%7CCEMPLOYEE%3DEXT011%7C')",
"type": "sapbyd.RPHCMRPMU01_Q1001QueryResult"
},
"CCALENDAR_DAY": "/Date(1582243200000)/",
"CCALENDAR_MONTH": "02/2020",
"CCALENDAR_WEEK": "08/2020",
"CEMPLOYEE": "EXT011",
"KCGROSS_CAPACITY": "8.00000000000000",
"KCVACATION": "0.00000000000000",
"UCGROSS_CAPACITY": "HUR",
"UCVACATION": "HUR"
},
{
"__metadata": {
"uri": "https://xxxx.sapbydesign.com/sap/byd/odata/ana_businessanalytics_analytics.svc/RPHCMRPMU01_Q1001QueryResults('%7CCCALENDAR_DAY%3D02%2F24%2F2020%7CCCALENDAR_MONTH%3D02%2F2020%7CCCALENDAR_WEEK%3D09%2F2020%7CCEMPLOYEE%3DEXT011%7C')",
"type": "sapbyd.RPHCMRPMU01_Q1001QueryResult"
},
"CCALENDAR_DAY": "/Date(1582502400000)/",
"CCALENDAR_MONTH": "02/2020",
"CCALENDAR_WEEK": "09/2020",
"CEMPLOYEE": "EXT011",
"KCGROSS_CAPACITY": "8.00000000000000",
"KCVACATION": "0.00000000000000",
"UCGROSS_CAPACITY": "HUR",
"UCVACATION": "HUR"
},
{
"__metadata": {
"uri": "https://xxxx.sapbydesign.com/sap/byd/odata/ana_businessanalytics_analytics.svc/RPHCMRPMU01_Q1001QueryResults('%7CCCALENDAR_DAY%3D02%2F25%2F2020%7CCCALENDAR_MONTH%3D02%2F2020%7CCCALENDAR_WEEK%3D09%2F2020%7CCEMPLOYEE%3DEXT011%7C')",
"type": "sapbyd.RPHCMRPMU01_Q1001QueryResult"
},
"CCALENDAR_DAY": "/Date(1582588800000)/",
"CCALENDAR_MONTH": "02/2020",
"CCALENDAR_WEEK": "09/2020",
"CEMPLOYEE": "EXT011",
"KCGROSS_CAPACITY": "8.00000000000000",
"KCVACATION": "0.00000000000000",
"UCGROSS_CAPACITY": "HUR",
"UCVACATION": "HUR"
}]
Я хотел бы сначала сгруппировать и связать их по ID сотрудника CEMPLOYEE
:
let e = _.chain(data.d.results)
// Group the elements of the result based on `CEMPLOYEE` property
.groupBy("CEMPLOYEE")
// `key` is group's name (CEMPLOYEE), `value` is the array of objects
.map((value, key) => ({
Emp: key,
Capacity: value
})).value();
Как и ожидалось , Я получил желаемый первый результат, например:
Emp: "E1001"
Capacity: (62) [{…}, {…}, {…}, {…}, {…}]
__proto__: Object
Теперь, когда я застрял, я бы сделал то же самое снова в моем узле Capacity,
Что я пытаюсь чтобы достичь, и мой конечный желаемый результат будет выглядеть следующим образом:
Emp: "E1001"
Capacity: (62) [
Week: "09/2020"
CapacityPerWeek: (5) [{…}, {…}, {…}, {…}, {…}
etc...
]
__proto__: Object
Итак, я попытался:
let e = _.chain(data.d.results)
// Group the elements of the result based on `CEMPLOYEE` property
.groupBy("CEMPLOYEE")
// `key` is group's name (CEMPLOYEE), `value` is the array of objects
.map((value, key) => ({
Emp: key,
Capacity: value
})).value();
_.forEach(e, function (capacityItem) {
capacityItem.ChainedCapacity = _.chain(capacityItem.Capacity)
// Group the elements of the result based on `CCALENDAR_WEEK` property
.groupBy("CCALENDAR_WEEK")
// `key` is group's name (CCALENDAR_WEEK), `value` is the array of objects
.map((value, key) => ({
Week: key,
CapacityPerWeek: value
})).value();
});
Но он дал мне раковый вывод, он излишне зациклен на моих предметах
Как этого добиться без зацикливания?