【问题标题】:Sum two values in Javascript Object将 Javascript 对象中的两个值相加
【发布时间】:2018-07-25 14:12:08
【问题描述】:

如果我有这样的 javascript 对象:

sampleObject[
   {
    suite: "Clubs",
    weight : 10
   },
   {
    suite: "Spades",
    weight : 6
   },
   {
    suite: "Hearts",
    weight : 2
   }
];

我将如何找到权重属性的总和?

【问题讨论】:

  • var sum=0; sampleObject.forEach(function(obj){ sum += obj.weight; });
  • 你尝试过什么,到底有什么问题?
  • data.reduce((r, {weight}) => r + weight, 0)
  • 很少有大多数人在这里为bike-shed 选择相同的颜色...

标签: javascript arrays object


【解决方案1】:

简单的总和减少应该可以工作。

var sum = sampleObject.reduce(( sum, card ) => sum + card.weight, 0 );

【讨论】:

  • 有趣,我不知道reduce方法的最后一个参数。
  • 第二个是初始值。这使得该函数适用于长度为 1 的数组,如果您不使用初始值,它将失败。以这种方式使用复杂的累加器也很方便,无需进行样板检查。
  • 感谢您的帮助,我最初尝试了一个 reduce 函数,但我的参数设置为 (a,b)。不知道可以给参数加属性,再次感谢
【解决方案2】:

你可以map你的对象然后reduce它:

  1. samplObject => [weight0, weight1, weight2]
  2. [weight0, weight1, weight2] => weight0 + weight1 + weight2

这意味着您将初始列表转换为权重列表,然后将值逐一求和。

const sampleObject = [
  {
    suite: "Clubs",
    weight : 10
  },
  {
    suite: "Spades",
    weight : 6
  },
  {
    suite: "Hearts",
    weight : 2
  }
];

let sum = sampleObject.map( el=> el.weight).reduce( (a,b) => a+b);
console.log(sum)

输出:

18

注意:

在此特定示例中,map 是开销。您可以轻松计算总和:

let sum = sampleObject.reduce( (a,b) => a+b.weight,0);

但是对于更复杂的数据结构,一般来说,最好记住map-reduce 的概念。

【讨论】:

  • 为什么是map?就在头顶。
  • 是的,在这个最小示例中它是开销。但是如果 OP 有一个更复杂的结构,比如嵌套对象,那么在脑海里有一个像 map-reduce 这样的一般概念会很好。
  • 那么也许做一个更好的例子/描述。只是一个提示。在当前示例中,这是不必要的。
  • 还是不明白。你说的复杂是什么意思?即使要总结的价值在其他地方或更深的地方,您也可以只使用reduce。例如weight: [{value: 10}] 只需要将reduce 操作更改为a + b.weight[0].value。因此,每当map 在这里有用时,我都会对示例非常感兴趣。
【解决方案3】:

使用reduce方法并将初始值作为0传递。您也可以使用普通for循环或forEach方法

let sampleObject = [{
    suite: "Clubs",
    weight: 10
  },
  {
    suite: "Spades",
    weight: 6
  },
  {
    suite: "Hearts",
    weight: 2
  }
];

let sum = sampleObject.reduce(function(acc, curr) {
  //Initially the value of acc will be 0
 // curr is the current object in context
  acc += curr.weight
   return acc;
}, 0)
console.log(sum)

【讨论】:

    【解决方案4】:

    使用Array.reduce

    sampleObject = [
       {
        suite: "Clubs",
        weight : 10
       },
       {
        suite: "Spades",
        weight : 6
       },
       {
        suite: "Hearts",
        weight : 2
       }
    ];
    
    var sum = sampleObject.reduce((a, b) => a + b.weight, 0);
    
    console.log(sum);

    【讨论】:

      【解决方案5】:

      使用reduce:

      let sampleObject = [{
        suite: "Clubs",
        weight : 10
      }, {
        suite: "Spades",
        weight : 6
      }, {
        suite: "Hearts",
        weight : 2
      }];
      
      let sum = sampleObject.reduce((a, b) => a + b.weight, 0);
      console.log(sum);

      【讨论】:

        【解决方案6】:

        由于您有一个对象数组,您可以通过执行以下操作访问第一个“权重”变量:sampleObject[0].weight。考虑到这一点,你可以用一个简单的 for 循环来总结所有这些:

        var sum = 0
        
        for(var i = 0; i < sampleObject.length; i+){
          sum += sampleObject[i].weight
        } 
        console.log(sum)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-11
          • 2019-02-20
          • 2018-04-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-06
          • 1970-01-01
          相关资源
          最近更新 更多