i am working on an array that represents a gantt schedule. Each object in the array has specific properties
{
id: string;
startTime: Date;
durationEstimated: number;
isBreak: boolean;
}
and some generalized values. I have to iterate through the array and adjust startTime based on previous values, taking into account the "isBreak" property (which remains static - startTime/durationEstimated never changes)
For instance, if my array looks like this:
[
{id: '1', startTime: "2020-04-01T08:30:00", durationEstimated: 60, isBreak: false},
{id: '2', startTime: "2020-04-01T09:00:00", durationEstimated: 15, isBreak: true},
{id: '3', startTime: "2020-04-01T09:45:00", durationEstimated: 60, isBreak: false},
{id: '4', startTime: "2020-04-01T10:45:00", durationEstimated: 60, isBreak: false},
{id: '5', startTime: "2020-04-01T11:45:00", durationEstimated: 60, isBreak: false},
{id: '6', startTime: "2020-04-01T12:00:00", durationEstimated: 60, isBreak: true},
{id: '7', startTime: "2020-04-01T13:45:00", durationEstimated: 60, isBreak: false}
]
The first item(id='1') runs for 30 mins, followed by break(id='2') for 15 mins, then completes the last 30 mins before moving on to the next item(id='3'). (New items will always be added after position 0)
If I need to add another object (Starttime does not matter)
{id: '8', startTime: "2022-05-01T14:30:00", durationEstimated: 60, isBreak: false}
Inserting this object at index 1 would result in the following array:
[
{id: '1', startTime: "2020-04-01T09:30:00", durationEstimated: 60, isBreak: false},
{id: '8', startTime: "2022-05-01T14:30:00", durationEstimated: 60, isBreak: false},
{id: '2', startTime: "2020-04-01T09:00:00", durationEstimated: 15, isBreak: true},
{id: '3', startTime: "2020-04-01T09:45:00", durationEstimated: 60, isBreak: false},
{id: '4', startTime: "2020-04-01T10:45:00", durationEstimated: 60, isBreak: false},
{id: '5', startTime: "2020-04-01T11:45:00", durationEstimated: 60, isBreak: false},
{id: '6', startTime: "2020-04-01T12:00:00", durationEstimated: 60, isBreak: true},
{id: '7', startTime: "2020-04-01T13:45:00", durationEstimated: 60, isBreak: false}
]
This is where I start sorting and updating the startTime of all elements after the first one to accommodate breaks.
Expected output:
[
{id: '1', startTime: "2020-04-01T08:30:00", durationEstimated: 60, isBreak: false},
{id: '2', startTime: "2020-04-01T09:00:00", durationEstimated: 15, isBreak: true},
{id: '8', startTime: "2020-04-01T09:45:00", durationEstimated: 60, isBreak: false},
{id: '3', startTime: "2020-04-01T10:45:00", durationEstimated: 60, isBreak: false},
{id: '4', startTime: "2020-04-01T11:45:00", durationEstimated: 60, isBreak: false},
{id: '6', startTime: "2020-04-01T12:00:00", durationEstimated: 60, isBreak: true},
{id: '5', startTime: "2020-04-01T13:45:00", durationEstimated: 60, isBreak: false},
{id: '7', startTime: "2020-04-01T14:45:00", durationEstimated: 60, isBreak: false}
]
The actual array consists of around 60-80 rows, with multiple breaks and different durationEstimated values.
Tried
I face challenges when rearranging items in the array to consider break times.
My approach involves iterating through the array, comparing each item against the previous item (startTime+duration), and adding the new date to the current item's startTime. The issue arises when dealing with breaks as they remain constant and do not change.
I have managed to handle this when only appending new items at the end of the array (since no sorting is required, just checking the previous value). However, in reality, new items can be inserted at any position.