Javascript 数组中对象的总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35480773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sum values of objects in array
提问by chimichanga
I would like to create a sum of an array with multiple objects.
我想创建一个包含多个对象的数组的总和。
Here is an example:
var array = [{"adults":2,"children":3},{"adults":2,"children":1}];
下面是一个例子:
var array = [{"adults":2,"children":3},{"adults":2,"children":1}];
How do I return the sum of adults and the sum of children into a new variable for each?
如何将成人的总和和儿童的总和返回到每个的新变量中?
Thanks, c.
谢谢,C。
回答by Rayon
Use
Array.prototype.reduce()
, the reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
使用
Array.prototype.reduce()
,reduce() 方法对累加器和数组的每个值(从左到右)应用一个函数以将其减少为单个值。
var array = [{
"adults": 2,
"children": 3
}, {
"adults": 2,
"children": 1
}];
var val = array.reduce(function(previousValue, currentValue) {
return {
adults: previousValue.adults + currentValue.adults,
children: previousValue.children + currentValue.children
}
});
console.log(val);
回答by DINESH Adhikari
array = [{"adults":2,"children":3},{"adults":2,"children":1}];
var totalChild = this.array.reduce((accum,item) => accum + item.children, 0)
console.log(totalChild) //output 4
回答by Foxcode
Seeing nobody posted this yet... here is a nice shorthand method:
看到还没有人发布这个……这是一个很好的速记方法:
.reduce((acc, curr) => acc + curr.property, 0)
Example:
例子:
arr = [{x:1, y:-1}, {x:2, y:-2}, {x:3, y:-3}];
x_sum = arr.reduce((acc, curr) => acc + curr.x, 0); // 6
y_sum = arr.reduce((acc, curr) => acc + curr.y, 0); // -6
回答by ZigGreen
var array = [{"adults":2,"children":3},{"adults":2,"children":1}];
var sumProps = prop => (sum, obj) => sum += obj[prop];
var adultsCount = array.reduce( sumProps('adults'));
var childrenCount = array.reduce( sumProps('children'));
回答by Dawid Pura
We are not writing code for you, but I suppose you should try:
我们不是为您编写代码,但我想您应该尝试:
var val = array.reduce(function (sum, tuple) {
return { adults: sum.adults + tuple.adults,
children: sum.children + tuple.children };
});
回答by Nina Scholz
You can write a function for this task, which gets the array to iterate over an the property, which value should be added.
你可以为这个任务编写一个函数,它让数组迭代一个属性,应该添加哪个值。
The key feature of the function is the Array#reducemethode and a property which returns the actual count calue and the actual property value.
该函数的关键特性是Array#reduce方法和一个返回实际计数值和实际属性值的属性。
function count(array, key) {
return array.reduce(function (r, a) {
return r + a[key];
}, 0);
}
var array = [{ "adults": 2, "children": 3 }, { "adults": 2, "children": 2 }],
adults = count(array, 'adults'),
children = count(array, 'children');
document.write('Adults: ' + adults + '<br>');
document.write('Children: ' + children + '<br>');
回答by RomanPerekhrest
To get a sum of each essence into separate variable:
要将每个本质的总和转化为单独的变量:
var array = [{"adults":2,"children":3},{"adults":2,"children":1}];
var adults_sum = 0, children_sum = 0;
array.forEach(function(obj){
adults_sum += obj["adults"];
children_sum += obj["children"];
});
console.log(adults_sum, children_sum); // 4 4
回答by worlf ee
May be it can help you and this also without use reduce func.
也许它可以帮助你,这也不需要使用reduce func。
var sumAdult=0;
var sumChildren=0;
var array = [{
"adult": 2,
"children": 3
}, {
"adult": 2,
"children": 1
}];
function x(a,b){
return a+b;
}
for (y in array){
sumAdult=x(0, array[y].adult);
console.log( "adult :" + sumAdult);
sumChildren=x(0, array[y].children);
console.log( "children :" + sumChildren);
}