I am trying to use filter to make my life easier.
My goal: Change the closeTime.hours of filter openDay(TUESDAY)
I have an array of objects that looks like:
buildHours = [
{
"openDay": "MONDAY",
"openTime": {},
"closeDay": "MONDAY",
"closeTime": {
"hours": 24
}
},
{
"openDay": "TUESDAY",
"openTime": {
"hours": 9
},
"closeDay": "TUESDAY",
"closeTime": {
"hours": 17
}
},
]
How I am doing this right now is removing the "Tuesday" Object using
const tempObject = buildHours.filter(i => i.openDay !== "TUESDAY"); // Destroy Tues
const tempHours = buildHours.filter(i => i.openDay === "TUESDAY")[0]; // Save Tues Obj
tempHours.openTime.hours = e.value; // Change Hour
tempObject.push(tempHours); // Recombine
console.log(tempObject);
Then "rebuilding" the "TUESDAY" object and adding it back to the main object. Although it works .. It seems a little convoluted. Isn't there a way I can use a combination of filter() and set closeTime.hours = 17? I have tried 6 ways to Sunday and nothing seems to work.
Is the way I am doing it right now the preferred method? Remove the object, rebuild and combine?
Or is there a more elegant way to change the hours for Tuesday without destroying the object and rebuilding it in the process?