首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从typescript中的arraylist中获取最大日期

如何从typescript中的arraylist中获取最大日期
EN

Stack Overflow用户
提问于 2017-07-22 16:36:46
回答 3查看 1K关注 0票数 1

我需要数组元素列表中的最大日期: start_time属性

代码语言:javascript
运行
复制
{
  "jobs" : [ {
    "job_id" : 10,
    "users" : [ {
      "user_id" : 11,
      "data_points" : [ {
        "duration" : 0,
        "start_time" : "2017-07-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "abc@gmail.com.com"
      }, {
        "duration" : 0,
        "start_time" : "2017-07-21T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "rs022015@gmail.com.com"
      }, {
        "duration" : 0,
        "start_time" : "2017-07-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "rs022015@gmail.com.com"
      } ]
    } ]
  } ]
}
EN

Stack Overflow用户

发布于 2017-07-22 22:11:54

您可以使用此函数计算日期数组的最大日期:

代码语言:javascript
运行
复制
arr = ["2017-07-20T04:04:43.000Z", "2017-07-21T04:04:43.000Z","2017-07-20T04:04:43.000Z"];
new Date(Math.max.apply(null,
             arr
             .map(x => new Date(x))));

特定的解决方案(仅适用于包含单元素数组的特定JSON ):

获取数据点列表

代码语言:javascript
运行
复制
var data = {
  "jobs" : [ {
    "job_id" : 10,
    "users" : [ {
      "user_id" : 11,
      "data_points" : [ {
        "duration" : 0,
        "start_time" : "2017-07-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "abc@gmail.com.com"
      }, {
        "duration" : 0,
        "start_time" : "2017-07-21T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "rs022015@gmail.com.com"
      }, {
        "duration" : 0,
        "start_time" : "2017-07-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "rs022015@gmail.com.com"
      } ]
    } ]
  } ]
};

var d = data["jobs"][0]["users"][0]["data_points"];

var max_start_time = 
     new Date(Math.max.apply(null,
     d
       .map(z =>   
        z["start_time"]    
       ).map(w => new Date(w))));
       
console.log(max_start_time);

通用解决方案(您将获得一个由"job_id""user_id"索引的数组)。这等同于sql select job_id,user_id,max(start_time) from ...group by job_id,user_id

代码语言:javascript
运行
复制
var data = {
  "jobs" : [ 
  {
    "job_id" : 110,
    "users" : [ {
      "user_id" : 11,
      "data_points" : [ {
        "duration" : 0,
        "start_time" : "2017-07-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "abc@gmail.com.com"
      }, {
        "duration" : 0,
        "start_time" : "2017-07-21T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "rs022015@gmail.com.com"
      }, {
        "duration" : 0,
        "start_time" : "2017-07-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "rs022015@gmail.com.com"
      } ]
    } ]
  },
  {
    "job_id" : 111,
    "users" : [ {
      "user_id" : 12,
      "data_points" : [ {
        "duration" : 0,
        "start_time" : "2017-08-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "abc@gmail.com.com"
      }, {
        "duration" : 0,
        "start_time" : "2017-08-21T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "rs022015@gmail.com.com"
      }, {
        "duration" : 0,
        "start_time" : "2017-08-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "rs022015@gmail.com.com"
      } ]
    },
    {
      "user_id" : 13,
      "data_points" : [ {
        "duration" : 0,
        "start_time" : "2017-08-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "abc@gmail.com.com"
      }, {
        "duration" : 0,
        "start_time" : "2017-08-21T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "rs022015@gmail.com.com"
      }, {
        "duration" : 0,
        "start_time" : "2017-08-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "rs022015@gmail.com.com"
      } ]
    }]
  }]
};

var max_start_time = {};

var r=data["jobs"].forEach(job =>
  {
     if (job.hasOwnProperty("job_id")) {
       if (job.hasOwnProperty("users")) {
         job["users"].forEach(userData =>
           { 
             if (userData.hasOwnProperty("data_points")) {
               max_start_time[[job["job_id"],userData["user_id"]]]=
               new Date(Math.max.apply(null,
                 userData["data_points"]
                 .map(z =>   
                 z["start_time"]    
                 ).map(w => new Date(w))));        
             }
           }
         )
       } 
     }
  }
);

console.log(max_start_time);

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45252465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档