In the function formatHours()
below, I am trying to assign newHours
to selectedLocation.data.hours
(an object)
I am using the spread operator to clone the object, so that when the function finishes selectedLocation.data.hours
remains the same, but it doesn't. After this function runs, selectedLocationd.data.hours
is now equal to newHours
.
formatHours():
const formatHours = () => {
// ERROR: trying to clone selectedLocation.data.hours, but not working
const newHours = { ...selectedLocation.data.hours };
// loop through all hours and convert them to javascript date with .toDate()
for (const hour in newHours) {
newHours[hour][0] = newHours[hour][0].toDate();
newHours[hour][1] = newHours[hour][1].toDate();
}
// set hours state with updated hours
setHours(newHours);
};
Here's what selectedLocation.data.hours
looks like BEFORE passing it through formatHours()
:
(object of arrays of Firebase Firestore timestamps)
Here's what selectedLocation.data.hours
looks like AFTER passing it through formatHours()
:
(object of arrays of Javascript Date objects)
NOTE: selectedLocation.data.hours
should stay the same before and after the function, but doesn't. I must be missing something.