Вы также можете рассмотреть более функциональный подход и избегать полагаться на внешнюю область для выполнения вашего обновления: (я предполагаю, что на вашем примере это так)
Вот функция, которая ищет дату для поискав списке объектов времени.Если объект найден, он выполняет заданный обратный вызов:
/**
* Finds in given list of times, a time which date property is set to given date.
* When a time is found, pass it as an argument to given callback.
* When no time is found, callback is not executed
* @param date {string}
* @param times {time[]}
* @param cb {function}
*/
const whenCurrentDay = (date, times, cb) => {
const found = times.find(time => time.date === date);
found && cb(found);
};
Затем вы бы использовали его следующим образом:
whenCurrentDay('mon', weekTimes, time => console.log(`do something with ${time.hours}`));
whenCurrentDay('tue', weekTimes, time => console.log(`do something with ${time.hours}`));
whenCurrentDay('wed', weekTimes, time => console.log(`do something with ${time.hours}`));
whenCurrentDay('thu', weekTimes, time => console.log(`do something with ${time.hours}`));
whenCurrentDay('fri', weekTimes, time => console.log(`do something with ${time.hours}`));
whenCurrentDay('sat', weekTimes, time => console.log(`do something with ${time.hours}`));
whenCurrentDay('sun', weekTimes, time => console.log(`do something with ${time.hours}`));
Я бы признал, что это немного более многословно.