简体   繁体   English

如何总结对象数组中的所有项目?

[英]How to sum up all items in an object array?

I have an array of objects:我有一个对象数组:

var test = [{"a":1, "b":2, "c":3}, {"a":4, "b":5, "c":6},{"a":7, "b":8, "c":9}];

I would like to add up all the values in object b to get the total 15我想将对象 b 中的所有值相加得到总共 15

Use reduce instead like this:像这样使用reduce

 var test = [{"a":1, "b":2, "c":3}, {"a":4, "b":5, "c":6},{"a":7, "b":8, "c":9}]; var sum = test.reduce((s, o) => s + ob, 0); console.log(sum);

Note : This test.reduce((s, o) => s + ob, 0) uses an arrow function .注意:这个test.reduce((s, o) => s + ob, 0)使用了一个箭头函数 It's the same as:它与以下内容相同:

test.reduce(function(s, o) {
    return s + o.b;
}, 0);

The method to get a single value back from an array of values is Array#reduce , you give it an accumulator function and an optional starting value and it applies it to each element until it gets the final value:从值数组中取回单个值的方法是Array#reduce ,你给它一个累加器函数和一个可选的起始值,并将它应用于每个元素,直到它得到最终值:

var test = [{"a":1, "b":2, "c":3}, {"a":4, "b":5, "c":6},{"a":7, "b":8, "c":9}];

test
  .map(function pluckB(o) { return o.b }) // Take only the 'b' property
  // => [2,5,8]
  .reduce(function add(a, b) { return a + b }, 0); // Sum them, starting from 0
  // => 15

It can get even terser with ES2015 arrow functions :它可以通过 ES2015 箭头函数变得更简洁:

test.map(o => o.b).reduce((a, b) => a + b, 0)

And with a functional utility library such as Ramda , it's just a call to two functions:使用Ramda等函数式实用程序库,只需调用两个函数即可:

R.sum(R.pluck('b', test))

As @ibrahim mahrir may have wanted to hint at in the comments, in JavaScript it can be more performant to actually compose such operations together and only iterate through the array once instead of using a pipeline style.正如@ibrahim mahrir 可能想在评论中暗示的那样,在 JavaScript 中,将这些操作实际组合在一起并且只迭代一次数组而不是使用管道样式会更高效。 That's because .map , .filter and co.那是因为.map.filter和 co. create new array copies instead of modifying the array in place, and that instantiation has a cost.创建新的数组副本而不是就地修改数组,并且实例化是有代价的。
But unless this code is running in a very tight loop, the performance impact will be absolutely negligible compared to the increased maintainability of using the most readable solution.但是除非这段代码在一个非常紧密的循环中运行,与使用最易读的解决方案增加的可维护性相比,性能影响绝对可以忽略不计。

If you want to find the sum of each object in the array, iterate the array overwriting the current element with the sum of the values.如果要查找数组中每个对象的总和,请使用值的总和覆盖当前元素来迭代数组。

 var test = [{"a":1, "b":2, "c":3}, {"a":4, "b":5, "c":6},{"a":7, "b":8, "c":9}]; test.forEach((e, i, a) => a[i] = Object.values(e).reduce((x, y) => x + y, 0)); console.log(test)

If you want to find the sum of the values of the matching properties of each object in the array, iterate the array creating a new object and add the values based on the key.如果要查找数组中每个对象的匹配属性的值的总和,请迭代该数组创建一个新对象并根据键添加值。

 var test = [{"a":1, "b":2, "c":3}, {"a":4, "b":5, "c":6},{"a":7, "b":8, "c":9}]; var result = test.reduce((o, e) => { for(var i in e) o[i] = (o[i] || 0) + e[i]; return o; }, {}); console.log(result);

You'll notice that the values of the second object equal 15, as well as all of the values of the properties named "b", so it is not entirely clear what you're after here.您会注意到第二个对象的值等于 15,以及名为“b”的属性的所有值,因此您在这里并不完全清楚。

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

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