试图从数组中的对象中求和我的值
我有 3 个对象,在每个对象中都有两个字段,我在其中声明了值:
let items = [
{
id: 1,
label: 'test',
itemname: 'testitem',
pieces: 4,
weight: 0.02
},
{
id: 2,
label: 'test',
itemname: 'testitem',
pieces: 4,
weight: 0.02
},
{
id: 2,
label: 'test',
itemname: 'testitem',
pieces: 4,
weight: 0.02
}
];
因此,如果我是正确的,则重量总和将为 0.06,而碎片总和将为 12,两者相乘将为 0.72,这意味着我想将重量总和与碎片总和相乘。
我看到这样的例子:
const weight = items.reduce((prev, cur) => {
return prev + (cur.weight * cur.pieces);
}, 0);
console.log(weight);
在这个例子中,总和仅为 0.24。有人可以帮我吗
编辑:@Robby Cornelissen 的评论是正确的。我的想法是错误的。
拉丁的传说
炎炎设计
相关分类