我有100个对象的数组,像这样:
[{date: "2018-09-09", amount: 49},
{date: "2018-09-12", amount: 94},
{date: "2017-12-20", amount: 46},
...
{date: "2017-05-12", amount: 89}]
我还创建了两个函数,每个函数都可以将这些数据转换为:
[{amount: 49, month: "Sep"},
{amount: 94, month: "Sep"},
{amount: 46, month: "Dec"},
...
{amount: 89, month: "May"}]
或
[{amount: 49, year: "2018"},
{amount: 94, year: "2018"},
{amount: 46, year: "2017"},
...
{amount: 89, year: "2017"}]
因此,剩下的部分是在这些数组中聚合对象,以便数组看起来像:
[{amount: 149, month: "Jan"},
{amount: 294, month: "Feb"},
{amount: 446, month: "Mar"},
...Apr, May, etc.
{amount: 489, month: "Dec"}]
和
[{amount: 894, year: "2018"},
{amount: 946, year: "2017"}]
总计。
可以在此处找到代码:https://codepen.io/Heithwald/pen/LKJvVv
我想拥有一个功能,该功能适用于两个预配置的阵列。它将接受一个预配置的数组并返回一个聚合的数组:
const aggregator = (arrayToAggregate) => {
}
什么是最好的解决方案?
我已经尝试了GroupBy帖子中建议的所有解决方案以及在Google中找到的大多数其他解决方案,但是这些解决方案都无法解决我的问题,因为最终结果与我所需要的完全不同。