1

I need max date from the list of array elements: start_time property

{
  "jobs" : [ {
    "job_id" : 10,
    "users" : [ {
      "user_id" : 11,
      "data_points" : [ {
        "duration" : 0,
        "start_time" : "2017-07-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      }, {
        "duration" : 0,
        "start_time" : "2017-07-21T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      }, {
        "duration" : 0,
        "start_time" : "2017-07-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      } ]
    } ]
  } ]
}

3 Answers 3

1

You can use this function to compute the maximum date of an array of dates:

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))));

Specific solution (works only for this specific JSON containing 1-element arrays):

Get list of data points

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" : "[email protected]"
      }, {
        "duration" : 0,
        "start_time" : "2017-07-21T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      }, {
        "duration" : 0,
        "start_time" : "2017-07-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      } ]
    } ]
  } ]
};

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);

General solution (you get an array indexed by "job_id" and "user_id"). This would be the equivalent of something like the 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" : "[email protected]"
      }, {
        "duration" : 0,
        "start_time" : "2017-07-21T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      }, {
        "duration" : 0,
        "start_time" : "2017-07-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      } ]
    } ]
  },
  {
    "job_id" : 111,
    "users" : [ {
      "user_id" : 12,
      "data_points" : [ {
        "duration" : 0,
        "start_time" : "2017-08-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      }, {
        "duration" : 0,
        "start_time" : "2017-08-21T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      }, {
        "duration" : 0,
        "start_time" : "2017-08-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      } ]
    },
    {
      "user_id" : 13,
      "data_points" : [ {
        "duration" : 0,
        "start_time" : "2017-08-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      }, {
        "duration" : 0,
        "start_time" : "2017-08-21T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      }, {
        "duration" : 0,
        "start_time" : "2017-08-20T04:04:43.000Z",
        "datastream" : "GENERIC",
        "username" : "[email protected]"
      } ]
    }]
  }]
};

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);

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

var userSortedData=jobs[0].users[0]. data_points.sort(function(a, b){return (b.start_time >a.start_time)?1:-1 ;});
console.log(userSortedData[0]);  // max Date

2 Comments

now, I want to display all keys and values, please help me how to achieve it (on every response keys are different)
the sorted array in the variable "userSortedData" so you can get all keys and values from it.
0

This should do it. What it does is go trough each list (jobs, users, data_points) and reduce every one of them to the max date representing it.

let data = {
    "jobs": [{
        "job_id": 10,
        "users": [{
            "user_id": 11,
            "data_points": [{
                "duration": 0,
                "start_time": "2017-07-20T04:04:43.000Z",
                "datastream": "GENERIC",
                "username": "[email protected]"
            }, {
                "duration": 0,
                "start_time": "2017-07-21T04:04:43.000Z",
                "datastream": "GENERIC",
                "username": "[email protected]"
            }, {
                "duration": 0,
                "start_time": "2017-07-20T04:04:43.000Z",
                "datastream": "GENERIC",
                "username": "[email protected]"
            }]
        }]
    }]
}

let max = data.jobs.reduce((prev, job) => {
    let maxJobStartTime = job.users.reduce((prev, user) => {
        let maxUserStartTime = user.data_points.reduce((prev, point) => {
            return prev - new Date(point.start_time).getTime() > 0 ? prev : new Date(point.start_time).getTime();
        }, 0)
        console.log(maxUserStartTime);
        return prev - maxUserStartTime > 0 ? prev : maxUserStartTime;
    }, 0)
    return prev - maxJobStartTime > 0 ? prev : maxJobStartTime;
}, 0)

console.log(new Date(max));

See a working playground example here.

A breakdown:

interface User {
    user_id: number,
    data_points: { start_time: string }[]
}

interface Job {
    job_id: number,
    users: User[]
}

interface Data {
    jobs: Job[]
}

// max time from a user's data_points
function maxUserStartTime(user: User) {
    return user.data_points.reduce((prev, point) => {
        return prev - new Date(point.start_time).getTime() > 0 ? prev : new Date(point.start_time).getTime();
    }, 0);
}

//using the max time of an user calculate the max time of all users in a job
function maxJobStartTime(job: Job) {
    return job.users.reduce((prev, user) => {
        let max = maxUserStartTime(user);
        return prev - max > 0 ? prev : max;
    }, 0);
}


// using the max time for a job calculate the max time out of all jobs
function maxTime(data: Data) {
    return data.jobs.reduce((prev, job) => {
        let max = maxJobStartTime(job)
        return prev - max > 0 ? prev : max;
    }, 0);
}

console.log(new Date(maxTime(data)));

A working example of the clean version can be found here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.