I have been working on loading test data into a javascript object and then passing it to my heating timers. While I have managed to make it work by individually inserting the code into each timer, I am looking to optimize my code and enhance my understanding.
Below is the code I'm using to set up my mock loaded data and how I'm parsing it into a javascript object:
var heating_data_load = '{ "monh1": "07", "monm1": "30", "mont1": "19", "monh2": "09", "monm2": "00", "mont2": "11", "monh3": "12", "monm3": "00", "mont3": "21", "monh4": "14", "monm4": "15", "mont4": "11", "monh5": "18", "monm5": "45", "mont5": "23", "monh6": "22", "monm6": "55", "mont6": "11"}';
var heating_data = JSON.parse(heating_data_load);
The issue I'm facing lies within the following function:
function load_deg_data() {
var switch_number = 6;
var days_of_week = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
for (var i = 0; i < days_of_week.length; i++) {
for (var t = 1; t != switch_number + 1; t++) {
var temphours = days_of_week[i] + "h" + t;
var tempmins = days_of_week[i] + "m" + t;
var tempdeg = days_of_week[i] + "t" + t;
var data_hour_load = heating_data.temphours;
var data_min_load = heating_data.tempmins;
var data_temp_load = heating_data.tempdeg;
var hour_load = '#' + days_of_week[i] + '_timer #hour_timer_' + t;
var min_load = '#' + days_of_week[i] + '_timer #min_timer_' + t;
var temp_load = '#' + days_of_week[i] + '_timer #temp_range_' + t;
$(hour_load).val(data_hour_load);
$(min_load).val(data_min_load);
$(temp_load).val(data_temp_load);
}
}
refresh_heat_timers();
};
The problematic line is currently:
var data_hour_load = heating_data.temphours;
The correct object should be heating_data.monh1 through monh6. I understand why my code is incorrect, but despite my efforts, I can't figure out how to loop through my data and dynamically set the variable temphours as part of the identifier for heating_data.
If anyone could assist me in understanding how to utilize a variable at the end of my heating_data object, I would greatly appreciate it.
I hope this explanation clarifies what I'm attempting to accomplish. Thank you kindly for your help.