[英]Find the average number of activities in the last 7 days
我有數據庫中包含的數據,通過查詢我可以讀取值(鍵)[日期,活動編號,其他值]
我應該能夠找到一種方法來獲取過去 7 天的平均活動數(顯然最后 7 天要與今天的日期進行比較)。
findStatistics() {
return ......
.remoteDB
.query(
'statistics2/statistics',
{
group: true,
}
)
.then(response => {
console.log("response ", response)
給了我這些價值觀:
response {
"rows": [
{
"key": [
"2019-10-28",
"02",
"20191028103516"
],
"value": 1
},
{
"key": [
"2019-10-31",
"05",
"20191031122358"
],
"value": 2
},
{
"key": [
"2019-11-05",
"01",
"20191105172615"
],
"value": 1
},
{
"key": [
"2019-11-05",
"01",
"20191105173005"
],
"value": 1
},
{
"key": [
"2019-11-05",
"01",
"2019115151417"
],
"value": 1
},
{
"key": [
"2019-11-05",
"03",
"20191105170543"
],
"value": 1
},
{
"key": [
"2019-11-05",
"05",
"20191031122358"
],
"value": 1
}
]
}
正如您在“key”中看到的,第一個值是日期,第二個是活動數。
我必須得到的是與每天活動數量相對應的平均值。
我能怎么做?? 謝謝你。
嘗試這個
const days = response.rows.reduce((acc, r) => {
const date = r.key[0]
if (!acc[date]) {
acc[date] = 1
} else {
acc[date] += 1
}
return acc
}, {})
const values = Object.values(days)
const avg = values.reduce((acc, d) => acc + d) / values.length
console.log(avg)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.