मान लें कि मेरे पास डेटा का यह सेट है:

"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"
            }]

मैं पहले समूह बनाना चाहता हूं और उन्हें कर्मचारी की आईडी से श्रृंखलाबद्ध करना चाहता हूं 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

अब मैं जो फंस गया हूं, मैं अपनी क्षमता नोड के अंदर फिर से वही काम करूंगा,

मैं जो हासिल करने की कोशिश कर रहा हूं और मेरा अंतिम वांछित आउटपुट इस तरह दिखेगा:

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

लेकिन इसने मुझे एक कैंसरयुक्त आउटपुट दिया, इसने अनावश्यक रूप से मेरी वस्तुओं पर कब्जा कर लिया

लूपिंग के बिना इसे कैसे प्राप्त करें?

0
napi15 2 अप्रैल 2020, 04:47

1 उत्तर

सबसे बढ़िया उत्तर

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}
1
xdeepakv 2 अप्रैल 2020, 22:41