【问题标题】:Sum Values of Object in a Array数组中对象的总和值
【发布时间】:2020-09-18 05:37:35
【问题描述】:

试图从我的数组中的对象中总结我的值

我有 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 的评论是正确的。我的想法是错误的。

【问题讨论】:

  • "[...] 所以如果我是对的,重量总和将是 0.06,而总和将是 12,两者乘以 0.72 [...]" 你说的不对。代码给出了正确的结果。 (a * b) + (c * d) != (a + c) * (b + d) 但这似乎并没有打扰那些回答这个问题的人,为您提供可以得出错误结果的代码。
  • 这是因为这发生在4*0.02 + 4*0.02 + 4*0.02 而不是这个(4+4+4)*(0.02+0.02+0.02)
  • 为什么要将两个和相乘?这不是你获得总重量的方式。
  • @RobbyCornelissen 我的错你是对的,在这种情况下它应该是 0.24,因为它必须首先将第一个项目与碎片或更好的数量相加,然后是下一个项目和下一个项目,谢谢,我想我不应该再熬夜了哈哈

标签: javascript arrays object


【解决方案1】:

如果您想要所有物品的总(总和)重量,您必须先获取每个单独物品的重量,然后将它们加在一起。

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)

【讨论】:

    【解决方案2】:

    您可以使用forEach 循环遍历数组,将piecesweight 值相加:

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

    然后乘以pieces * weight

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

    【讨论】:

    • 展示如何得到错误答案的意义何在?将这些总和相乘是没有意义的。
    • @Barmar 你是什么意思? OP 写了 “我想将重量的总和乘以碎片的总和”,这正是我的代码所做的。
    • 首先他写了“如果我是对的”。问题是他误解了他正在尝试做的基本数学。
    • 权重总和与件数总和相乘的应用是什么?对于任何有意义的事情,这怎么可能是正确的计算?他更新了问题以承认他误解了。
    • OP 写了 “所以如果我是对的,重量总和将是 0.06,而碎片总和将是 12”。 OP是对的:重量总和为0.06,件数总和为72。我按要求回答了问题。 我写完答案后,OP 更新问题以更改他们的要求。我不认为在我回答了他们的问题后,OP 改变了主意是我的错。如果我的答案不再回答已编辑的问题,我很乐意删除它,但我认为批评我的答案不再适用是不对的。
    【解决方案3】:

    使用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);

    【讨论】:

    • 如果你们所有人都可以停止提供导致错误结果的答案,那就太好了。
    猜你喜欢
    • 1970-01-01
    • 2021-03-15
    • 2016-09-25
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多