Home > database >  Array groupping with condition in javascript
Array groupping with condition in javascript

Time:01-09

So i have an array of response from BE with structure like this:

    const answer= [
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "34",
    "Shift Running": "3",
    "SKU Number": "310902",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "34",
    "Shift Running": "2",
    "SKU Number": "310902",
    "RPH Input": "Revisi"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "36",
    "Shift Running": "3",
    "SKU Number": "300360",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "36",
    "Shift Running": "3",
    "SKU Number": "310907",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "37",
    "Shift Running": "3",
    "SKU Number": "310908",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "11",
    "Line": "43",
    "Shift Running": "1",
    "SKU Number": "310101",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-06",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "11",
    "Line": "44",
    "Shift Running": "2",
    "SKU Number": "321294",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-06",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "11",
    "Line": "44",
    "Shift Running": "3",
    "SKU Number": "300079",
    "RPH Input": "Revisi"
  }
];

i try to grouped it into date with condition if the "Tanggal","Plant","Gedung/Zona","sektor",Line and SKU number is the same (excl. shift number) but the RPH input has value "Revisi", it will only group the value from RPH input that has value revisi instead of the Planned (H-1), with expected array to be like this, the value of the date is the accumulated value from Shift Running (here is the expected array):

const tranfrdata=[{ "2023-01-05": 12,
  "2023-01-06": 5 }]

it possible to just group it based on the logic i mention above, is that possible to do that? or any help on this?

CodePudding user response:

The idea here is to process Revisi items first and then ignore any subsequent items that have the same Tanggal, Plant, Gedung / Zona, Sektor, Line and SKU Number when accumulating the Shift Running number.

const answer = [{"Tanggal":"2023-01-05","Plant":"Ranc","Gedung / Zona":"Zona 2","Sektor":"9","Line":"34","Shift Running":"3","SKU Number":"310902","RPH Input":"Planned (H-1)"},{"Tanggal":"2023-01-05","Plant":"Ranc","Gedung / Zona":"Zona 2","Sektor":"9","Line":"34","Shift Running":"2","SKU Number":"310902","RPH Input":"Revisi"},{"Tanggal":"2023-01-05","Plant":"Ranc","Gedung / Zona":"Zona 2","Sektor":"9","Line":"36","Shift Running":"3","SKU Number":"300360","RPH Input":"Planned (H-1)"},{"Tanggal":"2023-01-05","Plant":"Ranc","Gedung / Zona":"Zona 2","Sektor":"9","Line":"36","Shift Running":"3","SKU Number":"310907","RPH Input":"Planned (H-1)"},{"Tanggal":"2023-01-05","Plant":"Ranc","Gedung / Zona":"Zona 2","Sektor":"9","Line":"37","Shift Running":"3","SKU Number":"310908","RPH Input":"Planned (H-1)"},{"Tanggal":"2023-01-05","Plant":"Ranc","Gedung / Zona":"Zona 2","Sektor":"11","Line":"43","Shift Running":"1","SKU Number":"310101","RPH Input":"Planned (H-1)"},{"Tanggal":"2023-01-06","Plant":"Ranc","Gedung / Zona":"Zona 2","Sektor":"11","Line":"44","Shift Running":"2","SKU Number":"321294","RPH Input":"Planned (H-1)"},{"Tanggal":"2023-01-06","Plant":"Ranc","Gedung / Zona":"Zona 2","Sektor":"11","Line":"44","Shift Running":"3","SKU Number":"300079","RPH Input":"Revisi"}];

const make_ident = ({
  Tanggal, Plant, ["Gedung / Zona"]: Zona, Sektor, Line, ["SKU Number"]: SKU
}) => {
  return Tanggal   '::'   Plant   '::'   Zona   '::'   Sektor   '::'  
    Line   '::'   SKU;
};

const sum_shifts_by_date = (data) => {
  // sort Revisi to the top of the array
  const sorted = [...data].sort((a, b) =>
    (a['RPH Input'] === b['RPH Input']) ? 0 :
      (a['RPH Input'] === 'Revisi' && b['RPH Input'] !== 'Revisi') ? -1 :
      1
  );
  
  const found = new Set();
  
  return sorted.reduce((acc, item) => {
    const { Tanggal, ["Shift Running"]: count } = item;
    const ident = make_ident(item);
    
    if(found.has(ident))
      return acc;
    
    // add ident to the found set so if we encounter the same details again
    // they'll be ignored (we've sorted Revisi to the top so we should be
    // ignoring any Planned (H-1) with the same details)
    found.add(ident);
    
    return { ...acc, [Tanggal]: (acc[Tanggal] || 0)   Number(count) };
  }, {});
};

console.log(sum_shifts_by_date(answer));

CodePudding user response:

  1. Create an empty object, groupedData, which will store the grouped data.
  2. Loop through the elements in the answer array.
  3. For each element, check if the Tanggal, Plant, Gedung / Zona, Sektor, Line, Shift Running, and SKU Number properties are the same as the current element. If they are, and the RPH Input is "Revisi", add the value of the Shift Running property to the groupedData object for the corresponding date. If the RPH Input is not "Revisi", skip the element.
  4. After the loop, the groupedData object will contain the grouped data. You can then use Object.entries(groupedData) to convert it to an array of key-value pairs, which you can then further manipulate as needed.

const answer= [
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "34",
    "Shift Running": "3",
    "SKU Number": "310902",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "34",
    "Shift Running": "2",
    "SKU Number": "310902",
    "RPH Input": "Revisi"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "36",
    "Shift Running": "3",
    "SKU Number": "300360",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "36",
    "Shift Running": "3",
    "SKU Number": "310907",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "37",
    "Shift Running": "3",
    "SKU Number": "310908",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "11",
    "Line": "43",
    "Shift Running": "1",
    "SKU Number": "310101",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-06",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "11",
    "Line": "43",
    "Shift Running": "2",
    "SKU Number": "321294",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-06",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "11",
    "Line": "44",
    "Shift Running": "3",
    "SKU Number": "300079",
    "RPH Input": "Planned (H-1)"
  }
]

const groupedData = {};

for (const element of answer) {
  if (element['RPH Input'] === 'Revisi') {
    if (
      element['Tanggal'] === element['Tanggal'] &&
      element['Plant'] === element['Plant'] &&
      element['Gedung / Zona'] === element['Gedung / Zona'] &&
      element['Sektor'] === element['Sektor'] &&
      element['Line'] === element['Line'] &&
      element['Shift Running'] === element['Shift Running'] &&
      element['SKU Number'] === element['SKU Number']
    ) {
      if (!groupedData[element['Tanggal']]) {
        groupedData[element['Tanggal']] = 0;
      }
      groupedData[element['Tanggal']]  = element['Shift Running'];
    }
  }
}

const tranfrdata = Object.entries(groupedData);
console.log(groupedData);

CodePudding user response:

If I understand your question correctly, I think the best way is to separate the array to Planned (H-1) and Revisi, then iterate the first array and check if it exists in the second array by using the SKU Number, and if it doesn't just accumulate from the first array, and if it does, accumulate from the second array:

const answer = [{
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "34",
    "Shift Running": "3",
    "SKU Number": "310902",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "34",
    "Shift Running": "2",
    "SKU Number": "310902",
    "RPH Input": "Revisi"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "36",
    "Shift Running": "3",
    "SKU Number": "300360",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "36",
    "Shift Running": "3",
    "SKU Number": "310907",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "9",
    "Line": "37",
    "Shift Running": "3",
    "SKU Number": "310908",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-05",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "11",
    "Line": "43",
    "Shift Running": "1",
    "SKU Number": "310101",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-06",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "11",
    "Line": "44",
    "Shift Running": "2",
    "SKU Number": "321294",
    "RPH Input": "Planned (H-1)"
  },
  {
    "Tanggal": "2023-01-06",
    "Plant": "Ranc",
    "Gedung / Zona": "Zona 2",
    "Sektor": "11",
    "Line": "44",
    "Shift Running": "3",
    "SKU Number": "300079",
    "RPH Input": "Revisi"
  }
];

const planned = answer.filter(obj => obj["RPH Input"] === "Planned (H-1)")
const revisi = answer.filter(obj => obj["RPH Input"] === "Revisi")

const transferData = answer.reduce((acc, curr) => {
  let compareObj, inPlanned
  if (curr["RPH Input"] === "Planned (H-1)") {
    compareObj = revisi.find(obj => obj["SKU Number"] === curr["SKU Number"])
  } else { // revisi
    inPlanned = planned.find(obj => obj["SKU Number"] === curr["SKU Number"])
  }


  let finalObj = curr
  if (compareObj) {
    finalObj = compareObj
  }

  if (!inPlanned) {
    return {
      ...acc,
      [finalObj["Tanggal"]]: finalObj["Tanggal"] in acc ? acc[finalObj["Tanggal"]]   Number(finalObj["Shift Running"]) : Number(finalObj["Shift Running"])
    }
  }
  return {
    ...acc,
  }
}, {})



console.log(transferData)

  • Related