繁体   English   中英

查找过去 7 天的平均活动数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM