【问题标题】:Using Moment to Combine a list of hourly dates by day使用 Moment 按天组合小时日期列表
【发布时间】:2019-07-14 19:31:20
【问题描述】:

我有[date, price]形式的价格/日期数据

let priceData = [
    [1551052800000, 0.33739737955243454]
    [1551139200000, 0.33628886196814234]
    [1551225600000, 0.12674156277665535]
    [1551312000000, 0.16847247989576378]
    [1557792000000, 0.5650889670671049]
    [1557878400000, 0.6003006017008962]
    [1557964800000, 0.6438789432408669]
    [1558051200000, 0.6684895789112406]
]

我想知道一种干净的方法来使用 lodash 或 map 或其他东西来获取这样的列表,并根据相同的日期将其组合起来......

// the data below may not be accurate, the point here is the structure
let exampleChuckedData = [
    [
            [1551052800000, 0.33739737955243454]
            [1551139200000, 0.33628886196814234]
            [1551225600000, 0.12674156277665535]
            [1551312000000, 0.16847247989576378]
    ]
    [
            [1557792000000, 0.5650889670671049]
            [1557878400000, 0.6003006017008962]
            [1557964800000, 0.6438789432408669]
            [1558051200000, 0.6684895789112406]
    ]
]

// Or more conceptually
// Grouped by same date
let exampleConceptData = [
    [
            ['01/01/2019', 0.33739737955243454]
            ['01/01/2019', 0.33628886196814234]
            ['01/01/2019', 0.12674156277665535]
            ['01/01/2019', 0.16847247989576378]
    ]
    [
            ['01/02/2019', 0.5650889670671049]
            ['01/02/2019', 0.6003006017008962]
            ['01/02/2019', 0.6438789432408669]
            ['01/02/2019', 0.6684895789112406]
    ]
]

我使用 moment 来满足我所有的日期格式化需求。也许有一种方法可以整合时刻来帮助解决这个问题,比如他们的*.isSame()

【问题讨论】:

    标签: javascript datetime momentjs lodash


    【解决方案1】:

    您可以使用_.groupBy(),并在回调中使用moment(date).day() 将每个日期转换为一天。使用_.values() 将组的对象转换为数组:

    const priceData = [[1551052800000,0.33739737955243454],[1551139200000,0.33628886196814234],[1551225600000,0.12674156277665535],[1551312000000,0.16847247989576378],[1557792000000,0.5650889670671049],[1557878400000,0.6003006017008962],[1557964800000,0.6438789432408669],[1558051200000,0.6684895789112406]]
    
    const result = _.values(_.groupBy(priceData, ([d]) => moment(d).startOf('day')
    ))
    
    console.log(result)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.14/lodash.js"></script>

    【讨论】:

    • 我喜欢这种方法。但它按一周中的同一天分组,而不是按同一日期分组。
    • 查看更新的答案。您的数据并没有真正展示这个想法,但它确实有效。
    • 就是这样!谢谢!
    【解决方案2】:

    您可以将 moment 与对象一起使用以按日期堆叠数据:

    第 1 步:只是创建一些虚拟数据以供使用

    const moment = require('moment');
    
    const data = [];
    
    for(let i = 0; i<1500; i++){
      let timestamp = moment().add(i, 'hours').format('X');
      data.push([timestamp, Math.random()*10000]);
    }
    

    所以现在有一个包含时间戳和随机数数据的数组。

    第 2 步:让我们按天堆叠。

    在这种情况下,对象会更优雅,因为您可以为保存数组的键指定当天的名称。这允许更快的比较机制,因为 javascript 可以比遍历数组查找匹配值更快地找到对象键。

    let stacked = {};
    
    for(let item of data){
      // Convert timestamp to nice format of string date
      const key = moment.unix(`${item[0]}`).format('YYYY-MMM-D');
      // If already exists just add it to the object key:value by first spreading what was there and then adding the new item.
      if(stacked[key]){
        stacked[key] = [...stacked[key], item];
      } else {
        // If new then set it from the start.
        stacked[key] = [item];
      }
    }
    

    就是这样,您的数据按此结构中的天数在对象中排序:

    data: {
      day1: [
        [100, 200],
        [101, 200]
      ],
      day2: [
        [200, 200],
        [201, 200]
      ]
      //...
    }
    

    Running sample

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多