感谢大佬开发,今天刚好也做了一个类似的需求,
[
{"印刷厂":"GHDW00000008","材料名称":"BOPP薄膜","厚度":1.7,"宽度":46,"米数":9775},
{"印刷厂":"GHDW00000008","材料名称":"BOPP薄膜","厚度":1.7,"宽度":46,"米数":6556},
{"印刷厂":"GHDW00000008","材料名称":"BOPP薄膜","厚度":1.7,"宽度":46,"米数":7856},
{"印刷厂":"GHDW00000007","材料名称":"BOPP薄膜","厚度":1.7,"宽度":46,"米数":7856},
{"印刷厂":"GHDW00000007","材料名称":"BOPP薄膜","厚度":1.7,"宽度":46,"米数":7856},
{"印刷厂":"GHDW00000007","材料名称":"BOPP薄膜2","厚度":1.7,"宽度":46,"米数":7856}
]
对上面的数据,根据印刷厂、材料名称、厚度、宽度这几个属性,汇总米数
代码- const data = [
- {"印刷厂":"GHDW00000008","材料名称":"BOPP薄膜","厚度":1.7,"宽度":46,"米数":9775},
- {"印刷厂":"GHDW00000008","材料名称":"BOPP薄膜","厚度":1.7,"宽度":46,"米数":6556},
- {"印刷厂":"GHDW00000008","材料名称":"BOPP薄膜","厚度":1.7,"宽度":46,"米数":7856},
- {"印刷厂":"GHDW00000007","材料名称":"BOPP薄膜","厚度":1.7,"宽度":46,"米数":7856},
- {"印刷厂":"GHDW00000007","材料名称":"BOPP薄膜","厚度":1.7,"宽度":46,"米数":7856},
- {"印刷厂":"GHDW00000007","材料名称":"BOPP薄膜2","厚度":1.7,"宽度":46,"米数":7856}
- ];
- const summarizedData = data.reduce((accumulator, currentItem) => {
- const key = `${currentItem.印刷厂}-${currentItem.材料名称}-${currentItem.厚度}-${currentItem.宽度}`;
- const existingItem = accumulator.find(item => item.key === key);
-
- if (existingItem) {
- existingItem.米数 += currentItem.米数;
- } else {
- accumulator.push({
- 印刷厂: currentItem.印刷厂,
- 材料名称: currentItem.材料名称,
- 厚度: currentItem.厚度,
- 宽度: currentItem.宽度,
- 米数: currentItem.米数,
- key // Helper key for grouping, won't be part of the final output
- });
- }
-
- return accumulator;
- }, []);
- // Remove the helper 'key' property from each object in the result array
- const cleanedData = summarizedData.map(item => {
- delete item.key;
- return item;
- });
- console.log(cleanedData);
复制代码
|