javascript对象数据聚合



我有一个javascript对象的例子如下:

0: {area: "F15 WET PROCESS", dissection_name: "WAIT_OTHERS", 2021-27: 5.3670104041}
1: {area: "F15 METROLOGY", dissection_name: "DELIVERY_TIME", 2021-27: 0}
2: {area: "F15 WET PROCESS", dissection_name: "WAIT_FOR_START_RUNNING", 2021-27: 0.1858817617}
3: {area: "F15 CMP", dissection_name: "BATCH_WAIT_TIME", 2021-27: 0}
4: {area: "F15 GENERAL", dissection_name: "WAIT_OTHERS", 2021-27: 0.0326702424}
5: {area: "F15 DRY ETCH", dissection_name: "DELIVERY_TIME", 2021-25: 0}
6: {area: "F15 DIFFUSION", dissection_name: "BATCH_WAIT_TIME", 2021-26: 1.544808954}
7: {area: "F15 METROLOGY", dissection_name: "WAIT_FOR_START_RUNNING", 2021-24: 0.0270019481}
8: {area: "F15 PHOTO", dissection_name: "BATCH_WAIT_TIME", 2021-24: 0.0001341795}
9: {area: "F15 DRY ETCH", dissection_name: "DELIVERY_TIME", 2021-24: 0}
10: {area: "F15 WET PROCESS", dissection_name: "BATCH_WAIT_TIME", 2021-24: 0.3487656399}...

每个数据块有3个参数。现在,因为第三个参数date不同,我想根据相同的'area'和'dissection_name'值将不同的date: value'聚合到相同的数据块中,例如:

0: {area: "F15 WET PROCESS", dissection_name: "WAIT_OTHERS",2021-24: 0.0001341795,2021-25: 0.0001341795,2021-26: 0.0001341795 2021-27: 5.3670104041...}
1: {area: "F15 WET PROCESS", dissection_name: "DELIVERY_TIME",2021-24: 0.0001341795,2021-25: 0.0001341795,2021-26: 0.0001341795 2021-27: 5.3670104041...}
2: {area: "F15 IMPLANT", dissection_name: "WAIT_OTHERS",2021-24: 0.0001341795,2021-25: 0.0001341795,2021-26: 0.0001341795 2021-27: 5.3670104041...}....

因此,对于每个相同的'area'和'dissection_name',数据块包含上述所有日期的参数。有办法做到这一点吗?任何编码细节提供将不胜感激。

尝试使用Array.reduce

const input = [
{ area: "F15 WET PROCESS", dissection_name: "WAIT_OTHERS", '2021-27': 5.3670104041 },
{ area: "F15 WET PROCESS", dissection_name: "WAIT_OTHERS", '2021-28': 5.3670104041 },
{ area: "F15 METROLOGY", dissection_name: "DELIVERY_TIME", '2021-27': 0 },
{ area: "F15 WET PROCESS", dissection_name: "WAIT_FOR_START_RUNNING", '2021-27': 0.1858817617 },
{ area: "F15 CMP", dissection_name: "BATCH_WAIT_TIME", '2021-27': 0 },
{ area: "F15 GENERAL", dissection_name: "WAIT_OTHERS", '2021-27': 0.0326702424 },
{ area: "F15 DRY ETCH", dissection_name: "DELIVERY_TIME", '2021-25': 0 },
{ area: "F15 DIFFUSION", dissection_name: "BATCH_WAIT_TIME", '2021-26': 1.544808954 },
{ area: "F15 METROLOGY", dissection_name: "WAIT_FOR_START_RUNNING", '2021-24': 0.0270019481 },
{ area: "F15 PHOTO", dissection_name: "BATCH_WAIT_TIME", '2021-24': 0.0001341795 },
{ area: "F15 DRY ETCH", dissection_name: "DELIVERY_TIME", '2021-24': 0 },
{ area: "F15 WET PROCESS", dissection_name: "BATCH_WAIT_TIME", ' 2021-24': 0.3487656399 },
];
const output = input.reduce((acc, curr)=> {
var matchingNode = acc.find((node) => node.area === curr.area && node.dissection_name === curr.dissection_name);
if (matchingNode) {
const matchingIndex = acc.findIndex((node) => node.area === curr.area && node.dissection_name === curr.dissection_name);
matchingNode = {...matchingNode, ...curr};
acc[matchingIndex] = matchingNode;
} else {
const newNode = { ...matchingNode, ...curr };
acc.push(newNode);
}
return acc;
}, []);
console.log(output);

最新更新