I have an array of objects that I call daysArray. This array represents 2 days as illustrated below:
console.log(daysArray)
Which logs out:
(2) [{…}, {…}]
…revealing an array of objects. When further expanded it reveals:
(2) [{…}, {…}]
0: {Wednesdays: Array(4)}
1: {Fridays: Array(4)}
…it reveals that the objects are two days. Lets further expand:
0:
Wednesdays: Array(4)
0: {T_04:00_Start: 1703034000, T_08:00_End: 1703048400}
1: {T_12:00_Start: 1703062800, T_16:00_End: 1703077200}
2: {T_16:00_Start: 1703077200, T_20:00_End: 1703091600}
3: {T_20:00_Start: 1703091600, T_00:00_End: 1703106000}
1:
Fridays: Array(4)
0: {T_04:00_Start: 1703206800, T_08:00_End: 1703221200}
1: {T_12:00_Start: 1703235600, T_16:00_End: 1703250000}
2: {T_16:00_Start: 1703250000, T_20:00_End: 1703264400}
3: {T_20:00_Start: 1703264400, T_00:00_End: 1703278800}
…it reveals that each day holds 4 time ranges. Each time range has a start time KEY and a timestamp associated to it and an end time KEY and a timestamp associated to it.
Note that the time range KEYS for both days (Fridays and Wednesdays) are all the same.
0: {T_04:00_Start: T_08:00_End:}
1: {T_12:00_Start: T_16:00_End:}
2: {T_16:00_Start: T_20:00_End:}
3: {T_20:00_Start: T_00:00_End:}
The following code is supposed to add an additional day to the daysArray array called Tuesdays, and most importantly, make sure that it only holds the following time ranges to keep it uniformed to the days already existing in the daysArray array being Wednesday and Fridays.
In the code, it is supposed to use the time ranges values in the removedKeys array to filter out (remove) the unwanted time ranges.
Find below my code:
const elementToInsert = "Tuesdays";
// Check if "Tuesdays" is not already in daysArray before proceeding
if (!daysArray.some(obj => Object.keys(obj)[0] === elementToInsert)) {
// Extract time range keys from tempRemovedTimeRanges for Tuesdays
const removedKeys = [...new Set(tempRemovedTimeRanges.flatMap(Object.values).flat().flatMap(Object.keys))];
console.log(removedKeys);
// Iterate through tempRemovedTimeRanges and remove corresponding time ranges from Tuesdays
for (const removedRangeKey of removedKeys) {
const dayObj = daysArray.find(obj => obj.hasOwnProperty(elementToInsert));
if (dayObj) {
const originalLength = dayObj[elementToInsert].length;
dayObj[elementToInsert] = dayObj[elementToInsert].filter(existingRange =>
!removedKeys.includes(removedRangeKey) ||
!Object.entries(existingRange).some(([key, value]) =>
removedRangeKey === key
)
);
const removedCount = originalLength - dayObj[elementToInsert].length;
console.log(`Removed ${removedCount} time ranges from ${elementToInsert}: `, removedRangeKey);
console.log(`Updated ${elementToInsert}: `, dayObj[elementToInsert]);
} else {
console.log(`${elementToInsert} not found in daysArray.`);
}
}
// Add "Tuesdays" along with its content back to daysArray
daysArray.push(...tempRemovedTuesdays);
console.log('Restored Tuesdays to daysArray: ', daysArray);
// Clear tempRemovedTuesdays after adding back to daysArray
tempRemovedTuesdays.length = 0;
// Check the everyday checkbox if all the checkboxes are checked!
if (daysArray.length === 7) {
const checkbox = document.getElementById("_EverydayEveryDay");
checkbox.checked = true;
checkbox.disabled = true;
}
} else {
console.log(`${elementToInsert} is already in daysArray.`);
}
The following logs out:
(4) ['T_00:00_Start', 'T_04:00_End', 'T_08:00_Start', 'T_12:00_End']
Tuesdays not found in daysArray.
Restored Tuesdays to daysArray: (3) [{…}, {…}, {…}]
Expanding on the daysArray reveals that Tuesdays was added:
(3) [{…}, {…}, {…}]
0: {Wednesdays: Array(4)}
1: {Fridays: Array(4)}
2: {Tuesdays: Array(6)}
However that the time ranges aren’t uniformed. Lets further expand
Fridays: Array(4)
0: {T_04:00_Start: 1703206800, T_08:00_End: 1703221200}
1: {T_12:00_Start: 1703235600, T_16:00_End: 1703250000}
2: {T_16:00_Start: 1703250000, T_20:00_End: 1703264400}
3: {T_20:00_Start: 1703264400, T_00:00_End: 1703278800}
2:
Tuesdays: Array(6)
0: {T_00:00_Start: 1702933200, T_04:00_End: 1702947600}
1: {T_04:00_Start: 1702947600, T_08:00_End: 1702962000}
2: {T_08:00_Start: 1702962000, T_12:00_End: 1702976400}
3: {T_12:00_Start: 1702976400, T_16:00_End: 1702990800}
4: {T_16:00_Start: 1702990800, T_20:00_End: 1703005200}
5: {T_20:00_Start: 1703005200, T_00:00_End: 1703019600}
The desired out come for ‘Tuesdays’ should be:
Tuesdays: Array(4)
0: {T_04:00_Start: 1702947600, T_08:00_End: 1702962000}
1: {T_12:00_Start: 1702976400, T_16:00_End: 1702990800}
2: {T_16:00_Start: 1702990800, T_20:00_End: 1703005200}
3: {T_20:00_Start: 1703005200, T_00:00_End: 1703019600}