Loda sh Как удвоить _.grouby набор данных и _.chain () их с помощью двойных ключей? - PullRequest
0 голосов
/ 02 апреля 2020

Предполагая, что у меня есть этот набор данных:

"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();
            });

Но он дал мне раковый вывод, он излишне зациклен на моих предметах

Как этого добиться без зацикливания?

1 Ответ

1 голос
/ 02 апреля 2020

Не уверен насчет lodash, если вы ищете vanilla JS. Пожалуйста, обратитесь к приведенному ниже коду.

const data =[{"__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"}];
const mapKeys = (obj, fn, en, cp) => {
  const data = obj.reduce((acc, k) => {
    const key = k[fn];
    let item = acc.find(x => x[en] === key);
    if (!item) {
      item = { [en]: key, [cp]: [] };
      acc.push(item);
    }
    item[cp].push(k);
    return acc;
  }, []);

  return data;
};

const funConvert = sample => {
  const data = mapKeys(sample, "CEMPLOYEE", "Emp", "Capacity");
  data.forEach(item => {
    item.Capacity = mapKeys(
      item.Capacity,
      "CCALENDAR_WEEK",
      "Week",
      "CapacityPerWeek"
    );
  });
  return data;
};
const emps = funConvert(data);
console.log(JSON.stringify(emps, null, 4));
.as-console-row {color: blue!important}
...