【问题标题】:Segregating array of objects based on hour-wise from time-stamp attribute根据时间戳属性按小时分离对象数组
【发布时间】:2017-10-18 06:02:42
【问题描述】:

我有一个对象数组,这些对象按小时组合。例如:

[{"id": "12345", "data": "abc", "timestamp": "2017-10-17T00:05:30.523Z"},
{"id": "16375", "data": "sgr", "timestamp": "2017-10-17T00:23:54.234Z"},
{"id": "46537", "data": "etd", "timestamp": "2017-10-17T01:36:16.463Z"},
{"id": "83645", "data": "eth", "timestamp": "2017-10-17T01:32:25.640Z"},
{"id": "36153", "data": "her", "timestamp": "2017-10-17T01:56:13.478Z"},
{"id": "31383", "data": "sry", "timestamp": "2017-10-17T05:56:56.362Z"},
{"id": "68123", "data": "rya", "timestamp": "2017-10-17T06:34:30.652Z"}]

我想要以下格式的结果数据:

{ "00:00-00:59": 
[{"id": "12345", "data": "abc", "timestamp": "2017-10-17T00:05:30.523Z"},
{"id": "16375", "data": "sgr", "timestamp": "2017-10-17T00:23:54.234Z"}]
"01:00-01:59": 
[{"id": "46537", "data": "etd", "timestamp": "2017-10-17T01:36:16.463Z"},
{"id": "83645", "data": "eth", "timestamp": "2017-10-17T01:32:25.640Z"},
{"id": "36153", "data": "her", "timestamp": "2017-10-17T01:56:13.478Z"}]
"05:00-05:59": [{"id": "31383", "data": "sry", "timestamp": "2017-10-17T05:56:56.362Z"}]
"06:00-06:59": [{"id": "68123", "data": "rya", "timestamp": "2017-10-17T06:34:30.652Z"}]

是否可以如上所述格式化数据?如何为需求编写短代码?

【问题讨论】:

    标签: javascript object timestamp arrayobject


    【解决方案1】:

    试试这个代码

    var a = [{"id": "12345", "data": "abc", "timestamp": "2017-10-   17T00:05:30.523Z"},
    {"id": "16375", "data": "sgr", "timestamp": "2017-10-17T00:23:54.234Z"},
    {"id": "46537", "data": "etd", "timestamp": "2017-10-17T01:36:16.463Z"},
    {"id": "83645", "data": "eth", "timestamp": "2017-10-17T01:32:25.640Z"},
    {"id": "36153", "data": "her", "timestamp": "2017-10-17T01:56:13.478Z"},
    {"id": "31383", "data": "sry", "timestamp": "2017-10-17T05:56:56.362Z"},
    {"id": "68123", "data": "rya", "timestamp": "2017-10-17T06:34:30.652Z"}]
    
    var t, h, n, obj = {};
    for(var i=0; i<a.length; i++) {
        t = new Date(a[i].timestamp);
    
        if ( !isNaN( t.getTime() ) ) { //if date is valid
            h = t.getHours();
            n = h + ':00-' + h + ':59';
            if(typeof obj[n] === 'undefined') obj[n] = [];
            obj[n].push(a[i]);
        }
    }
    
    console.log(obj);
    

    【讨论】:

    • 它有效,只需在日期构造函数之后检查t是否为有效日期。
    【解决方案2】:

    您可以使用Array.reduce 方法将您的数组转换为所需的对象。

    请参见下面的示例。

    var a = [{"id": "12345", "data": "abc", "timestamp": "2017-10-   17T00:05:30.523Z"},
    {"id": "16375", "data": "sgr", "timestamp": "2017-10-17T00:23:54.234Z"},
    {"id": "46537", "data": "etd", "timestamp": "2017-10-17T01:36:16.463Z"},
    {"id": "83645", "data": "eth", "timestamp": "2017-10-17T01:32:25.640Z"},
    {"id": "36153", "data": "her", "timestamp": "2017-10-17T01:56:13.478Z"},
    {"id": "31383", "data": "sry", "timestamp": "2017-10-17T05:56:56.362Z"},
    {"id": "68123", "data": "rya", "timestamp": "2017-10-17T06:34:30.652Z"}]
    
    var obj = a.reduce((acc, curr) => {
      var t = new Date(curr.timestamp);
      if(!isNaN(t.getTime())) {
        var h = t.getHours();
        var index = h + ':00-' + h + ':59';
        if(!acc[index]) {
          acc[index] = [];
        }
        acc[index].push(curr);
      }
      return acc;
    }, {});
    
    
    console.log(obj);

    【讨论】:

      【解决方案3】:

      Fireman26 的回答进行了一些小的更正:

      const data = [
          {"id": "12345", "data": "abc", "timestamp": "2017-10-17T00:05:30.523Z"},
          {"id": "16375", "data": "sgr", "timestamp": "2017-10-17T00:23:54.234Z"},
          {"id": "46537", "data": "etd", "timestamp": "2017-10-17T01:36:16.463Z"},
          {"id": "83645", "data": "eth", "timestamp": "2017-10-17T01:32:25.640Z"},
          {"id": "36153", "data": "her", "timestamp": "2017-10-17T01:56:13.478Z"},
          {"id": "31383", "data": "sry", "timestamp": "2017-10-17T05:56:56.362Z"},
          {"id": "68123", "data": "rya", "timestamp": "2017-10-17T06:34:30.652Z"}
      ]
      
      const grouped = data.reduce((obj, item) => {
          const hourString = String(new Date(item.timestamp).getUTCHours()).padStart(2, '0')
          if (hourString !== "NaN") {
              const key = `${hourString}:00-${hourString}:59`;
              (obj[key] = obj[key] || []).push(item)
          }
          return obj;
      }, {});
      
      1. 小时为 UTC 时区。
      2. 小时用“0”填充,即01:00-01:59而不是1:00-1:59

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-31
        • 1970-01-01
        • 1970-01-01
        • 2017-01-18
        • 2015-03-07
        • 1970-01-01
        • 2014-01-05
        相关资源
        最近更新 更多