javascript - 使用 LoDash 对时间序列数据进行分组和求和

标签 javascript node.js momentjs lodash

我想按 ISO 周对这些数据进行分组并汇总注册。我如何使用 LoDash 做到这一点?我最新的解决方案和示例数据如下所示:

const registrationJson = [ 
  { date: 2018-02-19T00:00:00.000Z, registrations: '7' },
  { date: 2018-02-20T00:00:00.000Z, registrations: '1' },
  { date: 2018-02-21T00:00:00.000Z, registrations: '3' },
  { date: 2018-02-22T00:00:00.000Z, registrations: '4' },
  { date: 2018-02-23T00:00:00.000Z, registrations: '1' },
  { date: 2018-02-28T00:00:00.000Z, registrations: '3' },
  { date: 2018-03-08T00:00:00.000Z, registrations: '1' } ]

const groupedResults = _(registrationJson)
  .groupBy("date")
  .map(item => {
    return {
      date: moment(item["date"])
        .startOf("isoWeek")
        .toString(),
      registrations: _.sumBy(item, "registrations")
    }
  })
  .value()

然而,这会返回错误的输出:

[ { date: 'Mon Mar 12 2018 00:00:00 GMT+0100',
    registrations: '7' },
  { date: 'Mon Mar 12 2018 00:00:00 GMT+0100',
    registrations: '1' },
  { date: 'Mon Mar 12 2018 00:00:00 GMT+0100',
    registrations: '3' },
  { date: 'Mon Mar 12 2018 00:00:00 GMT+0100',
    registrations: '4' },
  { date: 'Mon Mar 12 2018 00:00:00 GMT+0100',
    registrations: '1' },
  { date: 'Mon Mar 12 2018 00:00:00 GMT+0100',
    registrations: '3' },
  { date: 'Mon Mar 12 2018 00:00:00 GMT+0100',
    registrations: '1' } ]

最佳答案

您的代码的当前版本按以下方式工作:首先按日期(而不是按周!)分组,然后使用 map 转换元素。由于每个日期在该列表中都是唯一的,因此您的分组是无用的。 以相反的顺序进行:

const registrationJson = [ 
      { date: 2018-02-19T00:00:00.000Z, registrations: '7' },
      { date: 2018-02-20T00:00:00.000Z, registrations: '1' },
      { date: 2018-02-21T00:00:00.000Z, registrations: '3' },
      { date: 2018-02-22T00:00:00.000Z, registrations: '4' },
      { date: 2018-02-23T00:00:00.000Z, registrations: '1' },
      { date: 2018-02-28T00:00:00.000Z, registrations: '3' },
      { date: 2018-03-08T00:00:00.000Z, registrations: '1' } ]

const groupedResults = _(registrationJson)
      .map(item => {
          return {
              date: moment(item["date"])
                    .startOf("isoWeek")
                    .toString(),
               registrations: parseInt(item.registrations)
          }
      })
      .groupBy("date")
      .mapValues(item => {
           /* 
            here for each isoWeek start as a key you will get 
            list of elements for the same week; item is an array
           */
           return _.sumBy(item, 'registrations');
       })
      .value()

还要注意数据类型。由于您的 registrations 是一个字符串,它可能会导致连接而不是求和。

关于javascript - 使用 LoDash 对时间序列数据进行分组和求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49241285/

相关文章:

javascript - js中如何删除div中的元素?

javascript - 如何使用 javascript 或 jquery 验证单个文本字段中的多个电子邮件地址

javascript - Facebook 重定向到一个空白的权限请求页面

java - 从我的 Android 应用程序中的网页获取点击事件

node.js - 当我创建一个带有引用另一个 Mongoose 对象的位置的模式时,如何防止 Mongoose 创建 id?

javascript - 了解 momentjs 中 + 运算符的类型转换

validation - 使用 MomentJS 验证日期格式(不是日期字符串)?

node.js - 将对象附加到 Node.js 进程

javascript - Node MySQL 转义 LIKE 语句

javascript - 比较 moment.js 中的 24 小时格式时间