[英]Find the sum of values of array of objects with same name in angular2
I have a requirement where i need to find sum the values of amt in different objects having same name. 我有一个要求,我需要在具有相同名称的不同对象中找到amt的值之和。 Below is the code snippet
下面是代码片段
traveler = [
{ description: 'Senior', Amount: 50},
{ description: 'Senior', Amount: 50},
{ description: 'Adult', Amount: 75},
{ description: 'Child', Amount: 35},
{ description: 'Infant', Amount: 25 },
];
Here i want to caluclate the total sum of Amount in different objects with same description. 在这里,我想用相同的描述计算不同对象中金额的总和。 eg: object 0 and 1 contains same description 'Senior' so the total amt is 100
例如:对象0和1包含相同的描述'Senior',因此总amt为100
How to achieve this in angular2? 如何在angular2中实现呢?
Should i use inner forloops or is there any better approach? 我应该使用内部forloops还是有更好的方法? Please help me
请帮我
You can use reduce
to group the array into an object. 您可以使用
reduce
将数组分组为一个对象。
let traveler = [{"description":"Senior","Amount":50},{"description":"Senior","Amount":50},{"description":"Adult","Amount":75},{"description":"Child","Amount":35},{"description":"Infant","Amount":25}] let result = traveler.reduce((c, v) => { c[v.description] = (c[v.description] || 0) + v.Amount; return c; }, {}); console.log(result);
You could take a Map
for collecting the values and render an array of objects with the grouped result. 您可以使用
Map
来收集值,并使用分组的结果渲染对象数组。
var traveler = [{ description: 'Senior', amount: 50 }, { description: 'Senior', amount: 50 }, { description: 'Adult', amount: 75 }, { description: 'Child', amount: 35 }, { description: 'Infant', amount: 25 }], grouped = Array.from( traveler.reduce( (m, { description, amount }) => m.set(description, (m.get(description) || 0) + amount), new Map ).entries(), ([description, amount]) => ({ description, amount }) ); console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I think you should use array#reduce
method to do something like this perhaps: 我认为您应该使用
array#reduce
方法执行以下操作:
let traveler = [ { description: 'Senior', Amount: 50}, { description: 'Senior', Amount: 50}, { description: 'Adult', Amount: 75}, { description: 'Child', Amount: 35}, { description: 'Infant', Amount: 25 }, ]; function sumFinder(description) { return traveler.reduce((sum, e) => { (e.description == description) ? (sum += e.Amount) : (sum += 0) return sum; }, 0); } console.log(sumFinder('Senior'));
Use Array.reduce()
to get this output: 使用
Array.reduce()
获得以下输出:
var traveler = [ { description: 'Senior', Amount: 50}, { description: 'Senior', Amount: 50}, { description: 'Adult', Amount: 75}, { description: 'Child', Amount: 35}, { description: 'Infant', Amount: 25 }, ]; var res = traveler.reduce((acc, obj)=>{ var existItem = acc.find(item => item.description === obj.description); if(existItem){ existItem.Amount += obj.Amount; return acc; } acc.push(obj); return acc; }, []); console.log(res);
You can do it using rxjs among others: 您可以使用rxjs来实现:
travelers = [
{ description: 'Senior', amount: 50},
{ description: 'Senior', amount: 50},
{ description: 'Adult', amount: 75},
{ description: 'Child', amount: 35},
{ description: 'Infant', amount: 25 }
];
//emit each person
const source = Rx.Observable.from(travelers);
//group by description
const example = source
.groupBy(traveler => traveler.description)
//return each item in group as array
.mergeMap(group => group.toArray())
const reducer = (accumulator, currentValue) => {return accumulator + currentValue.amount};
const subscribe = example.subscribe(
val =>
console.log(
{
description: val[0].description,
amount: val.reduce( reducer, 0 )
}
)
);
This code's output is what you want: 此代码的输出是您想要的:
[object Object] {
amount: 100,
description: "Senior"
}
[object Object] {
amount: 75,
description: "Adult"
}
[object Object] {
amount: 35,
description: "Child"
}
[object Object] {
amount: 25,
description: "Infant"
}
Finally, here is a JSBin you can play with: http://jsbin.com/kacoqulike/1/edit?js,console 最后,这是一个您可以使用的JSBin: http ://jsbin.com/kacoqulike/1/edit?js,console
A clean and simple solution with Array.prototype.reduce
and ES6 Object assignment desctructuring: 使用
Array.prototype.reduce
和ES6对象分配解构的一种干净简单的解决方案:
const traveler = [{"description":"Senior","Amount":50},{"description":"Senior","Amount":50},{"description":"Adult","Amount":75},{"description":"Child","Amount":35},{"description":"Infant","Amount":25}]
const result = traveler.reduce((all, {description: d, Amount: a}) => {
all[d] = (all[d] || 0) + a;
return all;
}, {});
console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.