0

What's the easiest way to find the earliest start date and latest end date from the object below?

(Sorry - I realize there are a lot of similar questions already out there, but my JS skills are poor and I haven't been able to apply any of the solutions to my own data. So with that said, a code example would definitely help me out in any answers - thanks!!)

var ganttData = [
    {
        "id": 123456,
        "name": "Sample Project",
        "start": new Date(2010,11,6),
        "end": new Date(2011,0,6),
        "status": "Not Started",
        "phase": [
            {
                "id": 123457,
                "name": "Sample Phase",
                "start": new Date(2010,11,6),
                "end": new Date(2010,11,13),
                "status": "Not Started",
                "task": [
                    {
                        "id": 123458,
                        "name": "Sample Task",
                        "start": new Date(2010,11,6),
                        "end": new Date(2010,11,8),
                        "status": "Not Started"
                    }
                ]
            },
            {
                "id": 123459,
                "name": "Another Phase",
                "start": new Date(2010,11,13),
                "end": new Date(2011,0,20),
                "status": "Not Started"
            }
        ]
    }
]
4
  • Thanks. Any input as to why it isn't and what would make it JSON? Commented Oct 10, 2011 at 21:20
  • 2
    JSON is textual data exchange format. In JavaScript, JSON can only exist inside a string. See json.org. What you have is simply an array of objects. Object literals might look similar to JSON but they are not the same. Commented Oct 10, 2011 at 21:21
  • Still an array of objects. JSON would be e.g. var json = '{"foo": "bar"}'; which you then have to parse with JSON.parse into a JavaScript object in order to access foo. Be careful with just changing your question. Comparing the dates as strings is different than comparing Date objects. Commented Oct 10, 2011 at 21:38
  • For the purpose of this project I guess I'm less interested in it being JSON than just being able to loop through and get the data I need. I'll get my head around out JSON later. Reset dates back to 'Date' objects to make it easier to parse. Commented Oct 11, 2011 at 12:52

2 Answers 2

5

You could simply traverse the tree recursively

var max = new Date(-100000000*86400000);
var min = new Date( 100000000*86400000);

function compare(key,value) {
    if (key == "start" && value < min)
        min=value;
    else if (key == "end" && value > max)
        max=value;
}

function traverse(obj, fun) {
    for (prop in obj) {
        fun.apply(this,[prop, obj[prop]]);   
        if (typeof(obj[prop]) == "object") {
            traverse(obj[prop], fun);
        }
    }
}

traverse(ganttData, compare);

> max
Thu Jan 20 2011 00:00:00 GMT+0100 (W. Europe Standard Time)
> min
Mon Dec 06 2010 00:00:00 GMT+0100 (W. Europe Standard Time)

The above worked until you changed start and end from being a Date to being a string. Now you have to do something like this

arr = "2010,11,13".split(",");
date = new Date(arr[0], arr[1], arr[2]);

before you compare.

I got the reversed min and max dates from the JavaScript Reference.

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

1 Comment

Simple enough and it looks like it'll be easy to perform additional checks as well. Thank you!! Huge help.
1
function getEarliestAndLatest(ganttData) {
    var earliest = ganttData.start,
        latest   = ganttData.end,
        phase,
        task;
    for (var i = 0, countPhases = ganttData.phase.length; i < countPhases; i++) {
        phase = ganttData.phase[i];
        if (phase.start < earliest) {
            earliest = phase.start;
        }
        if (phase.end > latest) {
            latest = phase.end;
        }
        if (typeof phase.task !== 'undefined') {
            for (var j = 0, countTasks = phase.task.length; j < countTasks; j++) {
                task = phase.task[j];
                if (task.start < earliest) {
                    earliest = task.start;
                }
                if (task.end > latest) {
                    latest = task.end;
                }
            }
        }
    }
    return { earliest: earliest, latest: latest };
}

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.