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

Interactive pop-up windows in Bootstrap

I encountered an issue with bootstrap modal forms where the form displays correctly after clicking on the button, but the area in which the form is displayed gets blocked! It's difficult to explain, but you can see the problem by visiting this link. ...

Connect my Vue.js model data to the object that will be used for submission

I need help figuring out how to bind data from my input field into my Vue.js model. I've tried putting the UnitName inside the model but it's not working. Can someone provide some guidance on how to achieve this? new Vue({ el: "#app", da ...

extract the ng-model data and send it to an angular directive

In my current project, I have a specific situation where there are multiple text-boxes and when any of them is clicked, the corresponding ng-model should be displayed in the browser console. To achieve this functionality, I came up with the following Angul ...

The catch block is triggered when the dispatch function is called within the try block

It's strange how the code below works perfectly without any errors and records a response once the loginHandler() function is triggered. However, when I include the dispatch function inside the try block after receiving the response, the catch block e ...

Unable to retrieve the JSON response sent by the REST API within an HTML page

My ajax function is unable to properly receive the JSON content generated by a REST API. The REST API successfully creates the JSON data, but when passed to my ajax function, it does not work as expected. function loadJsonData(){ var dropDownValue = ...

Difficulty encountered while managing dropdown functionality in Protractor using TypeScript

I'm encountering some difficulties when it comes to selecting a dropdown in Protractor. Here's the structure of my DOM: https://i.stack.imgur.com/qK8sT.png This is the XPath I'm using to select the dropdown with the value "Yes": //label[ ...

Adding items to a JSON document

My task involves creating a pseudo cart page where clicking on checkout triggers a request to a JSON file named "ordersTest.json" with the structure: { "orders": [] }. The goal is to add the data from the post request into the orders array within the JSO ...

Tips for recognizing the click, such as determining which specific button was pressed

Currently, I am utilizing Angular 6. On the customer list page, there are three buttons - "NEW", "EDIT", and "VIEW" - all of which render to one component. As a result, it is crucial for me to determine which specific button has been clicked in order to ...

The script is malfunctioning on Google Chrome

Below is a script that I have: EXAMPLE : <script> var ul = document.getElementById("foo2"); var items = ul.getElementsByTagName("li"); for (var i = 0; i < items.length; i=i+2) { // perform an action on items[i], which repr ...

Steps for separating data within a CSV cell and generating additional rows

I just recently started learning how to code in Python. After searching through various resources, I couldn't find a direct solution to my current issue. Here's the problem I'm facing: I have a csv file structured like this: Header1, Heade ...

Sliding with JavaScript

I'm looking to develop a unique web interface where users can divide a "timeline" into multiple segments. Start|-----------------------------|End ^flag one ^flag two Users should be able to add customizable flags and adjust their position ...

The anticipated reply was supposed to consist of an array, however it ended up being an

I'm brand new to Angular and I've been searching for a solution to my issue with no luck. My app is supposed to retrieve data from a MongoDB database and display it to the user. However, I keep getting this error message: Error: [$resource:bad ...

There is a button on my website that uses a small piece of Javascript to post a tweet, but unfortunately, it is not functional on mobile devices

Check out this link to view the demonstration - http://codepen.io/illpill/pen/VbeVEq function sendTweet(message, author) { window.open('https://twitter.com/intent/tweet?hashtags=thequotemachine&text=' + encodeURIComponent('"' + m ...

Adding a task to my database successfully through Postman integration

I'm having trouble implementing the code for my todo app using React. I am encountering an issue with adding a new todo to the database using the handleSubmit function. Oddly enough, it works fine when testing with Postman, but not when trying to inpu ...

Working with JSON data in Angular 2 constructor

When sending a JSON response from the server, it is in the format shown below: {id: Int, name: String, childJSON: String} I want to map this data to the following TypeScript classes: export class Student{ constructor(public id: string, ...

Angular: Exploring the differences between $rootScope variable and event handling

I have a dilemma with an app that handles user logins. As is common in many apps, I need to alter the header once the user logs in. The main file (index.html) utilizes ng-include to incorporate the header.html I've come across two potential solution ...

Sorting arrays in Typescript

Is there a way to alphabetically sort an array of objects in Typescript based on a specific field? The array I have currently looks like this - https://i.stack.imgur.com/fQ3PA.png I'm interested in sorting it alphabetically by the 'channel' ...

Adjust the color of text as you scroll

Could you provide guidance on changing the color of fixed texts in specific sections of my portfolio website? Unfortunately, I can't share my lengthy code here, but would greatly appreciate it if you could illustrate with examples. Here's a refer ...

Removing undesired entries from a table view using AngularJs

In my table, there is a column called status which could have values like 'Open', 'Closed', 'Verified' and 'Rejected'. I am looking for a way to implement a filter in ng-repeat that will hide the rows with the statu ...

Is it Possible to Achieve Callbacks From AJAX to PHP Despite the Inability to Serialize Closures?

Question How can I incorporate a PHP callback passed via AJAX, where the callback is executed by the page requested through AJAX? The Scenario Comments are submitted through AJAX with parameters serialized and encrypted for security. The issue arises wh ...