【发布时间】:2019-03-14 04:09:41
【问题描述】:
我有一个对象数组,如下所示:-
0: {Id: 0, count1: 5, count2: 10, yearMonth: "201803"}
1: {Id: 0, count1: 10, count2: 0, yearMonth: "201804"}
2: {Id: 1, count1: 900, count2: 200, yearMonth: "201805"}
3: {Id: 0, count1: 10, count2: 0, yearMonth: "201806"}
4: {Id: 1, count1: 100, count2: 100, yearMonth: "201807"}
5: {Id: 1, count1: 100, count2: 2, yearMonth: "201808"}
我想将相似的键相加得到 预期输出:
1: {Id: 0, count1: 25, count2: 10}
2: {Id: 1, count1: 1100, count2: 402}
是否可以使用任何数学运算或reduce或任何其他javascript方法来实现?
谢谢
【问题讨论】:
-
Is it possible to achieve it using any mathematical operation or reduce or any other javascript method?- 是 -
欢迎来到 SO,@Sac。您的问题被否决的原因是您的问题并不表明您已尝试自己解决此问题。您可能会发现阅读 how to ask a good question 上的帮助指南很有用。
标签: javascript arrays knockout.js grouping