简体   繁体   English

数组中对象的总和

[英]Sum Values of Object in a Array

trying to sum my values from my Object in my Array试图从我的数组中的对象中总结我的值

I have 3 Objects and in every Object there are two fields where i have values declared:我有 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
      }
    ];

so if i am right the weight sum would be 0.06 and the sum of the pieces would be 12, both multiplied that would be 0.72, that means i want to multiply the sum of the weight with the sum of the pieces.所以如果我是对的,重量总和将是 0.06,件数之和将是 12,两者相乘都是 0.72,这意味着我想将重量之和与件数之和相乘。

i saw examples like this:我看到这样的例子:

const weight = items.reduce((prev, cur) => {
  return prev + (cur.weight * cur.pieces);
}, 0);
console.log(weight);

with this example the total sum would be only 0.24.在这个例子中,总和仅为 0.24。 Could someone help me here out有人可以帮我吗

Edit: @Robby Cornelissen was right with his comment.编辑:@Robby Cornelissen 的评论是对的。 It was a wrong way of thinking on my part.就我而言,这是一种错误的思考方式。

If you want the total(sum) weight of all items, you will have to get the weight of each individual item first then add them all together.如果您想要所有物品的总(总和)重量,您必须先获得每个单独物品的重量,然后将它们加在一起。

const sum = (arr) => {
    return arr.map(item => item.pieces * item.weight).reduce((current, total) => current + total)
}

 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 } ] const sum = (arr) => { return arr.map(item => item.pieces * item.weight).reduce((current, total) => current + total) } const totalWeight = sum(items) console.log(totalWeight)

You can use forEach to loop through the array, adding up the pieces and weight values:您可以使用forEach循环遍历数组,将piecesweight值相加:

let pieces = 0; // sum of pieces
let weight = 0; // sum of weight
items.forEach(function(elmt){
    pieces += elmt.pieces;
    weight += elmt.weight;
});

Then multiply pieces * weight :然后乘以pieces * weight

let total = pieces * weight;
console.log(total);

Using class使用class

 let items=[{id:1,label:"test",itemname:"testitem",pieces:4,weight:.02},{id:2,label:"test",itemname:"testitem",pieces:4,weight:.02},{id:2,label:"test",itemname:"testitem",pieces:4,weight:.02}]; class total { weight = 0; pieces = 0; get product() { return this.weight * this.pieces; } } const totalObj = items.reduce((prev, cur) => { prev.weight += cur.weight; prev.pieces += cur.pieces; return prev; }, new total); console.log(totalObj.product);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM