I am trying to figure out how I can get the average price of every day.
const groupByDay = _.groupBy(nextProps.data, (date) => {
return moment.unix(date.createdAt).startOf("day").format();
});
const result = _.map(groupByDay, (group, day) => {
return {
date: day.split("T")[0],
offers: group // all offers for the day
}
});
const temp = [];
result.forEach((res) => {
res.offers.forEach((rr) => {
temp.push({date: res.date, avgPrice: rr.price});
})
});
This will return the following temp
array as seen below:
[
{
"date":"2020-01-07",
"avgPrice":334
},
{
"date":"2020-01-07",
"avgPrice":756
},
{
"date":"2020-01-02",
"avgPrice":30
}
]
But how I can make it group this and then calculate the average price of all objects with the same date
value? E.g. avgPrice
of 2020-01-07
should be 545
which is ((334+756)/2)
.