我需要数组元素列表中的最大日期: start_time属性
{
  "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"
      } ]
    } ]
  } ]
}发布于 2017-07-22 22:11:54
您可以使用此函数计算日期数组的最大日期:
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 ):
获取数据点列表
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
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);
https://stackoverflow.com/questions/45252465
复制相似问题