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?
3
Answers
You could group first and then get average per group and result.
You’ll need to know the amount of same dates before you can calculate the avarege.
Use reduce to add all displacement on the same date, keep track of the count.
Then map over the grouped result and caculate the avarage
You can group the items with a map:
And a benchmark:
Open in the playground