我有startTime和endTime的数组。我想从间隔数组中找到实际的开始时间和结束时间。该数组如下所示。
var shiftArray = [{"startTime":"14:00:00","endTime":"21:59:59"},
{"startTime":"06:00:00","endTime":"13:59:59"},
{"startTime":"22:00:00","endTime":"05:59:59"}];
预期结果是:
todayShift = {"startTime":"06:00:00", "endTime":"05:59:59"};
任何人都可以建议一些最佳选择来达到预期效果。
答案 0 :(得分:0)
我认为除了循环和比较之外还有其他方法
var shiftArray = [{
"startTime": "14:00:00",
"endTime": "21:59:59"
}, {
"startTime": "06:00:00",
"endTime": "13:59:59"
}, {
"startTime": "22:00:00",
"endTime": "05:59:59"
}];
var todayShift = {
"startTime": "23:59:59",
"endTime": "00:00:00"
};
shiftArray.forEach(function(shift) {
if (shift.startTime < todayShift.startTime) {
todayShift.startTime = shift.startTime;
}
if (shift.endTime > todayShift.endTime) {
todayShift.endTime = shift.endTime;
}
})
result.innerHTML = JSON.stringify(todayShift, null, 2)
&#13;
<pre id="result"></pre>
&#13;