英文:
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 = [{
"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)
<!-- 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 = [{"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));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论