Loading JavaScript variable with pre-processed JavaScript information

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.

Answer №1

Given that temphours is designated as a variable containing the desired property name, rather than the property itself, it is not possible to employ dot-notation for accessing said property. Accessing a property using a variable name can be achieved as follows:

var data_hour_load = heating_data[temphours];

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to increase the value of a multidimensional array

In my current code, I am attempting to increase the value of caps by 1 whenever an event with type 10 or 11 is encountered in a loop. However, when running the code, I am encountering an offset 1 error specifically on the line $red_stats[$i]['caps&apo ...

How can objects be merged within an array and a new object be inserted?

I have an array of objects representing podcasts in a podcast app. Here is how the data looks: [{ id: "uuid-1" timeInSeconds: 1000 dateListened: "2021-01-01T15:57:17.000Z" }, // <---same day { id: "uuid-2" timeInS ...

Enhancing jQuery mobile performance - optimizing page caching and eliminating unnecessary HTML elements from off-screen content

In an effort to increase the speed of my jQuery mobile web app, I stumbled upon the concept of page caching through a Google search. However, I am having trouble finding a clear explanation of what it does and why so many people are advocating for it to be ...

Attempting to route a selector, yet on the second attempt, the entity is successfully transferred

I am currently working on developing a function that will switch the image every half second for 10 iterations. During the final iteration, the image src will be set to the actual value. Everything seems to work fine for the first loop, however, when the s ...

Can we verify if this API response is accurate?

I am currently delving into the world of API's and developing a basic response for users when they hit an endpoint on my express app. One question that has been lingering in my mind is what constitutes a proper API response – must it always be an o ...

Dynamic form population with AJAX and PHP: Populate various input fields using a single AJAX request

I recently discovered how to load data into a text box using AJAX and successfully populated a dropdown select box as well. However, I'm facing an issue where I can't populate both text boxes and select boxes simultaneously with just one AJAX cal ...

Is there a way for me to adjust the font size across the entire page?

Most CSS classes come with a fixed font-size value already set. For instance: font-size: 16px font-size: 14px etc. Is there a way to increase the font size of the entire page by 110%? For example, font-size: 16px -> 17.6 font-size: 14px -> 15.4 ...

Utilizing the sub() function in jq to dynamically modify JSON values based on specified conditions

I'm in need of modifying certain values within JSON data and integrating it into an existing shell script. My aim is to utilize jq for this task, specifically the "sub()" function to trim a portion of a string value. Executing the following command l ...

Resize the main container to fit the floated elements

I've been working on constructing a family tree, and the last part of the functionality is proving to be quite challenging for me. My family tree consists of list elements that are all floated to the left. Currently, when the tree expands beyond the ...

3D Animation for All Browsers

I'm currently working on creating a small browser-based game and I've been experimenting with animations to get the desired effect. So far, the animation works well in Opera and Edge, although there is a slight cropping issue in Edge. Unfortunat ...

Is there a way to ensure that an external file function completes before proceeding with the main code execution?

In my project, I have two key JavaScript files: main.js and load.js. Within load.js, there are two essential functions - getData, which makes an AJAX post call, and constHTML, which appends data based on the JSON array returned from the AJAX call. This app ...

Using Javascript to set up a callback that alerts when a script file is done loading with the attributes "async" and "defer"

My app is loading the platform.js file asynchronously with the attributes of async defer. <script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer> </script> I am looking for a callback function that will alert m ...

Exploring the Use of data- Attributes in SVG Circle Elements

Looking for a way to dynamically update the color of a Circle element in your SVG when it is clicked? You can achieve this by using jQuery's .css() method in conjunction with the data-* attribute. CHECK OUT AN EXAMPLE: STYLING IN CSS svg { height ...

In PHP, upload a .dat file so that it can be saved as a JSON file

I'm attempting to upload a .dat file and extract its content in JSON format. Here is the code I am using: HTML: <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="fileUpload"> < ...

Step-by-step guide on implementing a border-bottom for an active link

Is there a way to apply a border-bottom to btn_articles and btn_posts when one of them is clicked? I attempted to use the active class but it did not have the desired effect. Any suggestions or solutions would be greatly appreciated. let btn_articles = ...

Attempting to save data to an external JSON file by utilizing fs and express libraries

I've encountered a challenge while attempting to serialize an object into JSON. Despite my best efforts, I keep encountering an error that has proven to be quite stubborn... Below is the code snippet that's causing the issue: APP.post('/api ...

Ways to verify every entered word without having to click a button

My goal is to implement real-time word checking in a textarea using JavaScript/Angular. I want to validate each word as users are typing it out. What is the best approach for achieving this? ...

The error domain is alamofire.error.serialization.response with a code of -1011 indicating that the request has failed due to a bad request (400)

Utilizing the AFNetworking library to send data to the server. This is the code snippet I am using to send data to the server. - (void) callLoginAPI:(NSDictionary *)dictProfile{ // 1 NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKe ...

Automatically close the popup each time it is displayed (using jQuery/JavaScript)

I am having issues with auto-closing my popup each time I open it. The current code I have only closes the popup the first time, requiring me to refresh the browser in order to auto-close it again. Can someone please assist me in writing a new code that ...

Refreshing a Next.js page results in a 404 error

I've set up a Next.js page called page.js that can be accessed through the URL http://localhost:3000/page. However, I have a need to access this page through a different URL, namely http://localhost:3000/my-page. To achieve this, I decided to utilize ...