妄想社成员 发表于 2024-6-8 12:57:18

感谢大佬开发,今天刚好也做了一个类似的需求,
[
{"印刷厂":"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);

小萝卜David 发表于 2024-6-8 13:01:37

妄想社成员 发表于 2024-6-8 12:57
感谢大佬开发,今天刚好也做了一个类似的需求,
[
{"印刷厂":"GHDW00000008","材料名称":"BOPP薄膜","厚 ...

感觉走远了。

KinShing 发表于 2024-6-9 00:15:43

感谢分享:hjyzw:

赛龙周 发表于 2024-6-9 05:15:19

感谢分享
用对象工具插件也可以的

小萝卜David 发表于 2024-6-9 08:07:43

赛龙周 发表于 2024-6-9 05:15
感谢分享
用对象工具插件也可以的

贴图学习学习。

赛龙周 发表于 2024-6-9 18:05:20

小萝卜David 发表于 2024-6-9 08:07
贴图学习学习。

mock 模拟了10000条数据,年龄随机18-60


数组操作的select方法从对象中取出age数组

用sum对age数组求和

陈城 发表于 2024-6-9 23:06:19

厉害,学习一下

lizeming27 发表于 2024-6-11 18:33:35

和循环比这个快, 那和统计字段比呢? 哪个会更快一些?

爱上网络 发表于 2024-6-12 11:27:57

好东西:lol

恒美恒美 发表于 2024-6-12 14:59:18

学习学习,谢谢大佬分享
页: 1 [2] 3 4 5 6 7
查看完整版本: 【插件分享-数组属性求和】指定数组和属性,求和。