我解決這個問題:合併對象,並返回冷凝範圍對象
寫功能mergeRanges(),它滿足時間 範圍的陣列,並返回冷凝範圍(1 = 30分鐘的陣列)
Input:
[
{startTime: 0, endTime: 1}, // meeting from 10:00 – 10:30 am (1 = 30 minutes)
{startTime: 3, endTime: 5}, // meeting from 12:00 – 1:30 pm
{startTime: 4, endTime: 8},
{startTime: 10, endTime: 12},
{startTime: 9, endTime: 10},
]
Expected Output:
[
{startTime: 0, endTime: 1},
{startTime: 3, endTime: 8},
{startTime: 9, endTime: 12},
]
我的解決辦法是/如下:
var arr = [
{startTime: 0, endTime: 1},
{startTime: 3, endTime: 5},
{startTime: 4, endTime: 8},
{startTime: 10, endTime: 12},
{startTime: 9, endTime: 10},
];
function result(arr){
var finalResult = {};
var c = arr.sort(function(a,b){ //First Sort
return a.startTime - b.startTime;
});
var d = c.reduce(function(a,b){
//Logic
//check to see if start time lies between a[start] < b[startTime] < a[end]
// If the startTime lies between a[startTime] to b[endTime] -> Merge them.
// if(a[startTime]< b[startTime] < b[endTime]){ //if start time lies between start and end.
// a[endTime] = b[endTime];
// delete b;
// }
});
}
result(arr);
我無法理解如何使用reduce函數來交換邏輯。有人能夠啓發我嗎?
什麼叫 「交換邏輯」 是什麼意思? –