I have an array like this where an object is inside nested arrays:
let arr = [
[
{date: 1578787200000, displacement: 0},
{date: 1580860800000, displacement: 1},
{date: 1593302400000, displacement: 2},
{date: 1606780800000, displacement: 3}
],
[
{date: 1578787200000, displacement: 10},
{date: 1580860800000, displacement: 20},
{date: 1593302400000, displacement: 30},
{date: 1606780800000, displacement: 40}
]
]
and want to group the objects by ‘date’ whilst also averaging the ‘displacement’ values so the result looks like this:
[
{date: 1578787200000, displacement: 5},
{date: 1580860800000, displacement: 10.5},
{date: 1593302400000, displacement: 16.5},
{date: 1606780800000, displacement: 21.5}
]
I have tried .reduce() combined with .forEach() to sum the displacements per date but can’t get it to output exactly what I’m after.
e.g.
let res = arr.reduce(function (prev, currentArr) {
currentArr.forEach(function (obj) {
(prev.date == obj.date || []).push(prev.displacement + obj.displacement)
}, 0);
return prev;
});
What is the best way to accomplish this?