开发者

Finding max/min date in multidimensional JavaScript object

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,开发者_Python百科
        "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"
            }
        ]
    }
]


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.


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 };
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜