使用JavaScript进行带条件的数组分组。

huangapple go评论70阅读模式
英文:

Array groupping with condition in javascript

问题

以下是您要的翻译部分:

所以我有一个来自BE的响应数组结构如下

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""Plant""Gedung/Zona""Sektor"Line和SKU号码相同不包括班次号),但RPH输入具有值"Revisi"则只会分组具有值"Revisi"的RPH输入而不是"Planned (H-1)"期望的数组如下所示日期的值是Shift Running的累积值这是期望的数组):

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

是否可能根据我上面提到的逻辑对其进行分组是否可以做到这一点或者有什么帮助吗

请注意,我已经删除了HTML实体字符(如"),以便更好地呈现JavaScript对象。

英文:

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?

答案1

得分: 1

如果我正确理解你的问题,我认为最好的方法是将数组分为Planned (H-1)Revisi,然后遍历第一个数组,并使用SKU编号在第二个数组中检查是否存在,如果不存在,则从第一个数组累积,如果存在,则从第二个数组累积:

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"
  },
  // ... (其他数组项)

];

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

请注意,这段代码将按照指定的逻辑处理数组中的数据,并将结果打印到控制台。

英文:

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:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const answer = [{
&quot;Tanggal&quot;: &quot;2023-01-05&quot;,
&quot;Plant&quot;: &quot;Ranc&quot;,
&quot;Gedung / Zona&quot;: &quot;Zona 2&quot;,
&quot;Sektor&quot;: &quot;9&quot;,
&quot;Line&quot;: &quot;34&quot;,
&quot;Shift Running&quot;: &quot;3&quot;,
&quot;SKU Number&quot;: &quot;310902&quot;,
&quot;RPH Input&quot;: &quot;Planned (H-1)&quot;
},
{
&quot;Tanggal&quot;: &quot;2023-01-05&quot;,
&quot;Plant&quot;: &quot;Ranc&quot;,
&quot;Gedung / Zona&quot;: &quot;Zona 2&quot;,
&quot;Sektor&quot;: &quot;9&quot;,
&quot;Line&quot;: &quot;34&quot;,
&quot;Shift Running&quot;: &quot;2&quot;,
&quot;SKU Number&quot;: &quot;310902&quot;,
&quot;RPH Input&quot;: &quot;Revisi&quot;
},
{
&quot;Tanggal&quot;: &quot;2023-01-05&quot;,
&quot;Plant&quot;: &quot;Ranc&quot;,
&quot;Gedung / Zona&quot;: &quot;Zona 2&quot;,
&quot;Sektor&quot;: &quot;9&quot;,
&quot;Line&quot;: &quot;36&quot;,
&quot;Shift Running&quot;: &quot;3&quot;,
&quot;SKU Number&quot;: &quot;300360&quot;,
&quot;RPH Input&quot;: &quot;Planned (H-1)&quot;
},
{
&quot;Tanggal&quot;: &quot;2023-01-05&quot;,
&quot;Plant&quot;: &quot;Ranc&quot;,
&quot;Gedung / Zona&quot;: &quot;Zona 2&quot;,
&quot;Sektor&quot;: &quot;9&quot;,
&quot;Line&quot;: &quot;36&quot;,
&quot;Shift Running&quot;: &quot;3&quot;,
&quot;SKU Number&quot;: &quot;310907&quot;,
&quot;RPH Input&quot;: &quot;Planned (H-1)&quot;
},
{
&quot;Tanggal&quot;: &quot;2023-01-05&quot;,
&quot;Plant&quot;: &quot;Ranc&quot;,
&quot;Gedung / Zona&quot;: &quot;Zona 2&quot;,
&quot;Sektor&quot;: &quot;9&quot;,
&quot;Line&quot;: &quot;37&quot;,
&quot;Shift Running&quot;: &quot;3&quot;,
&quot;SKU Number&quot;: &quot;310908&quot;,
&quot;RPH Input&quot;: &quot;Planned (H-1)&quot;
},
{
&quot;Tanggal&quot;: &quot;2023-01-05&quot;,
&quot;Plant&quot;: &quot;Ranc&quot;,
&quot;Gedung / Zona&quot;: &quot;Zona 2&quot;,
&quot;Sektor&quot;: &quot;11&quot;,
&quot;Line&quot;: &quot;43&quot;,
&quot;Shift Running&quot;: &quot;1&quot;,
&quot;SKU Number&quot;: &quot;310101&quot;,
&quot;RPH Input&quot;: &quot;Planned (H-1)&quot;
},
{
&quot;Tanggal&quot;: &quot;2023-01-06&quot;,
&quot;Plant&quot;: &quot;Ranc&quot;,
&quot;Gedung / Zona&quot;: &quot;Zona 2&quot;,
&quot;Sektor&quot;: &quot;11&quot;,
&quot;Line&quot;: &quot;44&quot;,
&quot;Shift Running&quot;: &quot;2&quot;,
&quot;SKU Number&quot;: &quot;321294&quot;,
&quot;RPH Input&quot;: &quot;Planned (H-1)&quot;
},
{
&quot;Tanggal&quot;: &quot;2023-01-06&quot;,
&quot;Plant&quot;: &quot;Ranc&quot;,
&quot;Gedung / Zona&quot;: &quot;Zona 2&quot;,
&quot;Sektor&quot;: &quot;11&quot;,
&quot;Line&quot;: &quot;44&quot;,
&quot;Shift Running&quot;: &quot;3&quot;,
&quot;SKU Number&quot;: &quot;300079&quot;,
&quot;RPH Input&quot;: &quot;Revisi&quot;
}
];
const planned = answer.filter(obj =&gt; obj[&quot;RPH Input&quot;] === &quot;Planned (H-1)&quot;)
const revisi = answer.filter(obj =&gt; obj[&quot;RPH Input&quot;] === &quot;Revisi&quot;)
const transferData = answer.reduce((acc, curr) =&gt; {
let compareObj, inPlanned
if (curr[&quot;RPH Input&quot;] === &quot;Planned (H-1)&quot;) {
compareObj = revisi.find(obj =&gt; obj[&quot;SKU Number&quot;] === curr[&quot;SKU Number&quot;])
} else { // revisi
inPlanned = planned.find(obj =&gt; obj[&quot;SKU Number&quot;] === curr[&quot;SKU Number&quot;])
}
let finalObj = curr
if (compareObj) {
finalObj = compareObj
}
if (!inPlanned) {
return {
...acc,
[finalObj[&quot;Tanggal&quot;]]: finalObj[&quot;Tanggal&quot;] in acc ? acc[finalObj[&quot;Tanggal&quot;]] + Number(finalObj[&quot;Shift Running&quot;]) : Number(finalObj[&quot;Shift Running&quot;])
}
}
return {
...acc,
}
}, {})
console.log(transferData)

<!-- end snippet -->

答案2

得分: 1

这里的想法是首先处理Revisi项目,然后在累积Shift Running号码时忽略具有相同的Tanggal、Plant、Gedung / Zona、Sektor、Line和SKU 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) => {
  // 将Revisi排序到数组的顶部
  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;

    // 将ident添加到found集合中,以便如果我们再次遇到相同的详细信息,它们将被忽略(我们已经将Revisi排序到顶部,所以我们应该忽略具有相同详细信息的任何Planned (H-1))
    found.add(ident);

    return { ...acc, [Tanggal]: (acc[Tanggal] || 0) + Number(count) };
  }, {});
};

console.log(sum_shifts_by_date(answer));
英文:

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.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const answer = [{&quot;Tanggal&quot;:&quot;2023-01-05&quot;,&quot;Plant&quot;:&quot;Ranc&quot;,&quot;Gedung / Zona&quot;:&quot;Zona 2&quot;,&quot;Sektor&quot;:&quot;9&quot;,&quot;Line&quot;:&quot;34&quot;,&quot;Shift Running&quot;:&quot;3&quot;,&quot;SKU Number&quot;:&quot;310902&quot;,&quot;RPH Input&quot;:&quot;Planned (H-1)&quot;},{&quot;Tanggal&quot;:&quot;2023-01-05&quot;,&quot;Plant&quot;:&quot;Ranc&quot;,&quot;Gedung / Zona&quot;:&quot;Zona 2&quot;,&quot;Sektor&quot;:&quot;9&quot;,&quot;Line&quot;:&quot;34&quot;,&quot;Shift Running&quot;:&quot;2&quot;,&quot;SKU Number&quot;:&quot;310902&quot;,&quot;RPH Input&quot;:&quot;Revisi&quot;},{&quot;Tanggal&quot;:&quot;2023-01-05&quot;,&quot;Plant&quot;:&quot;Ranc&quot;,&quot;Gedung / Zona&quot;:&quot;Zona 2&quot;,&quot;Sektor&quot;:&quot;9&quot;,&quot;Line&quot;:&quot;36&quot;,&quot;Shift Running&quot;:&quot;3&quot;,&quot;SKU Number&quot;:&quot;300360&quot;,&quot;RPH Input&quot;:&quot;Planned (H-1)&quot;},{&quot;Tanggal&quot;:&quot;2023-01-05&quot;,&quot;Plant&quot;:&quot;Ranc&quot;,&quot;Gedung / Zona&quot;:&quot;Zona 2&quot;,&quot;Sektor&quot;:&quot;9&quot;,&quot;Line&quot;:&quot;36&quot;,&quot;Shift Running&quot;:&quot;3&quot;,&quot;SKU Number&quot;:&quot;310907&quot;,&quot;RPH Input&quot;:&quot;Planned (H-1)&quot;},{&quot;Tanggal&quot;:&quot;2023-01-05&quot;,&quot;Plant&quot;:&quot;Ranc&quot;,&quot;Gedung / Zona&quot;:&quot;Zona 2&quot;,&quot;Sektor&quot;:&quot;9&quot;,&quot;Line&quot;:&quot;37&quot;,&quot;Shift Running&quot;:&quot;3&quot;,&quot;SKU Number&quot;:&quot;310908&quot;,&quot;RPH Input&quot;:&quot;Planned (H-1)&quot;},{&quot;Tanggal&quot;:&quot;2023-01-05&quot;,&quot;Plant&quot;:&quot;Ranc&quot;,&quot;Gedung / Zona&quot;:&quot;Zona 2&quot;,&quot;Sektor&quot;:&quot;11&quot;,&quot;Line&quot;:&quot;43&quot;,&quot;Shift Running&quot;:&quot;1&quot;,&quot;SKU Number&quot;:&quot;310101&quot;,&quot;RPH Input&quot;:&quot;Planned (H-1)&quot;},{&quot;Tanggal&quot;:&quot;2023-01-06&quot;,&quot;Plant&quot;:&quot;Ranc&quot;,&quot;Gedung / Zona&quot;:&quot;Zona 2&quot;,&quot;Sektor&quot;:&quot;11&quot;,&quot;Line&quot;:&quot;44&quot;,&quot;Shift Running&quot;:&quot;2&quot;,&quot;SKU Number&quot;:&quot;321294&quot;,&quot;RPH Input&quot;:&quot;Planned (H-1)&quot;},{&quot;Tanggal&quot;:&quot;2023-01-06&quot;,&quot;Plant&quot;:&quot;Ranc&quot;,&quot;Gedung / Zona&quot;:&quot;Zona 2&quot;,&quot;Sektor&quot;:&quot;11&quot;,&quot;Line&quot;:&quot;44&quot;,&quot;Shift Running&quot;:&quot;3&quot;,&quot;SKU Number&quot;:&quot;300079&quot;,&quot;RPH Input&quot;:&quot;Revisi&quot;}];
const make_ident = ({
Tanggal, Plant, [&quot;Gedung / Zona&quot;]: Zona, Sektor, Line, [&quot;SKU Number&quot;]: SKU
}) =&gt; {
return Tanggal + &#39;::&#39; + Plant + &#39;::&#39; + Zona + &#39;::&#39; + Sektor + &#39;::&#39; +
Line + &#39;::&#39; + SKU;
};
const sum_shifts_by_date = (data) =&gt; {
// sort Revisi to the top of the array
const sorted = [...data].sort((a, b) =&gt;
(a[&#39;RPH Input&#39;] === b[&#39;RPH Input&#39;]) ? 0 :
(a[&#39;RPH Input&#39;] === &#39;Revisi&#39; &amp;&amp; b[&#39;RPH Input&#39;] !== &#39;Revisi&#39;) ? -1 :
1
);
const found = new Set();
return sorted.reduce((acc, item) =&gt; {
const { Tanggal, [&quot;Shift Running&quot;]: 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&#39;ll be ignored (we&#39;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));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月8日 21:57:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定