Using Javascript to pull data from an array using a variable

I am working with an Array and need to retrieve information for a specific day:

var today = new Date();
var Days = ['Sunday','Monday',"Tuesday","Wednesday","Thursday","Friday","Saturday"];
var Day = Days[today.getDay()];

alert(Schedule.Day[5].Subject);

When I directly input "Monday" into the alert, it works perfectly. However, when using the variable it does not work.

This is a portion of my array:

var Schedule = {
    Monday: [ {
       Subject: "0",
       Packing: "0"
     },
         {
       Subject: "1",
       Packing: "1"
     },
         {
       Subject: "2",
       Packing: "2"
     },
          {
       Subject: "3",
       Packing: "3"
     },
          {
       Subject: "4",
       Packing: "x"
     },
          {
       Subject: "xx5",
       Packing: "xx"
     },
          {
       Subject: "break",
       Packing: "break"
     },
         {
       Subject: "Sports",
       Packing: "Sports bag"
     },
          {
       Subject: "Sports",
       Packing: "Sports bag"
    }
   ],
            [.......]

Thank you!

Answer №1

One important thing you may have overlooked is the ability to access a hash element dynamically, meaning the key is contained in the variable Day. Instead of using the dot operator, you can retrieve a hash element by indicating its key within square brackets, as shown below:

alert(Stundenplan[Day][5].Fach);

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

Variety of properties determined by a "type" prop, expanding variations based on a value from the interface

I am trying to enhance a type based on a value from the main interface. If the type == multiline, it will have a specific interface, and if the type == icon, it will have a different type. import React, { memo, useCallback, ReactNode } from 'react&apo ...

How can I enter a single backslash for location input in node.js without using double backslashes?

I have a project where the user needs to input a word to search for in files along with the folder location. However, I am struggling to write code that allows the user to input the location using single backslashes instead of double backslashes. const fol ...

The request body contains no information

I'm having trouble debugging this issue. Can anyone help me out? When using console.log(req.body), I am getting an empty object {}. I've tried multiple approaches but still can't figure out the problem. Even after attempting to use middle ...

Images cannot be uploaded using ajax

I have been troubleshooting an issue with uploading image files using ajax without refreshing the page. However, I am facing a problem where the file does not get moved to the specified folder even after implementing basic PHP code for file upload. if($_S ...

Show full-screen images on click using Ionic framework

Currently, I am working on developing a mobile app using the Ionic framework. I have created a layout that resembles the one shown in this Card Layout. My question is: How can I make the card image display in full screen when clicked by the user and retur ...

I am unable to retrieve any text from the class as it is returning a null value

Can someone please assist me with two issues I am facing: 1) Firstly, I am trying to match the text of an alert pop-up using the following code: <div class="noty_message message"><span class="noty_text">Uh oh! Email or password is incorrect&l ...

Exploring the Differences Between Using CPU and GPU for Transformations in WebGL

I've just started exploring WebGL, and I'm curious about whether using JavaScript or a shader would be faster for transformations like rotation, translation, scale, etc. Can anyone shed some light on this? ...

Are there any other alternatives besides using var to declare global variables and functions?

While working on this page: I discovered an interesting aspect related to variable declaration. It appeared that using 'var' is essential for declaring global variables and functions; however, I attempted to use 'let' and 'const&a ...

Creating a dynamically allocated struct within an array that is nested inside another struct in the C language

Let me summarize this situation for you - I have a main struct called Catalog, which includes a counter (to keep track of the number of books it contains), capacity (the total number of books it can hold, that can also be expanded), and an array of pointer ...

`Can't figure out how to input variables into PHP Form`

As a newcomer to PHP, I apologize if this question seems obvious. I would like to create a form that gathers more information than just the typical "name," "email," and "message." I am specifically looking to include the following labels: (i) Will you be ...

Error: Jasmine unit test failed due to a connection refusal

While running the unit test in jasmine using the ng test command, an error occurred. Previously, everything was configured correctly and working well. I'm not sure why this error suddenly appeared. Error message in browser: Description: Connection ...

changing the sequence of key positions in an array of Objects can be done by specifying the desired key

I'm currently working on an export feature, but the data being received from the API is not in the correct sequence (the keys of the object are in an array). I need to rearrange the keys in the object so that they are in the desired order for a single ...

I am having trouble establishing a connection to the JavaScript MQTT server

Error Encountered: WebSocket Error 12031 - The Server Connection Was Reset In order to subscribe to MQTT messages from the user interface, the code below is being utilized. A Mosquitto broker is currently running on my local machine, with the IP address s ...

Invoke a method that accepts an array by reference

Here is a snippet of code I am working on: int main() { int workers; printf("How many workers are there?\n"); scanf("%d", &workers); printf("What are their preferences?\n"); int *pref = malloc(workers * sizeof(int)); if ( ...

Setting the Node environment as an environment variable within the application: a step-by-step guide

Struggling with setting process.env.NODE_ENV to TEST in my code. Check out the relevant snippets below: test.js import server from "../../src/index.js"; process.env.NODE_ENV = "test"; console.log(process.env.NODE_ENV); // outputs &qu ...

What strategies can be utilized to address the absence of an element in an array iteration that is currently ongoing?

Currently, I am looping through an array of strings using forEach() method to check if each element's length is even or odd. If the length is even, I am removing it using splice(). Although my conditions seem correct, when I look at the input and out ...

What is the best way to launch a Popup window that spans from the top to the bottom of the screen?

I'm attempting to create a popup window that stretches from the top of the screen to the "applications" bar in Windows. Here's the code I'm using: function windowOpener(windowHeight, windowWidth, windowName, windowUri) { var windowHeight = ...

Error: Reactjs - Attempting to access the 'name' property of an undefined variable

As I continue to learn about React, I have been experimenting with props in my code. However, I encountered an error where the prop is appearing as undefined. This issue has left me puzzled since I am still at a basic level of understanding React. If anyo ...

Initiating a Page with Dynamic Modal Window according to Backend Exigencies

Dear experts, can you help me with a problem? I want to implement a modal window on an vb.aspx page if a specific condition from the codebehind query is true. How can I achieve this? Thank you! ...

Retrieve the current date and time in JSON format using moment.js for the local

Below is the code snippet I am working with: var startTime = moment("2020-09-08 16:00:00").toDate(); console.log(startTime) const data = {start: startTime} console.log(JSON.stringify(data) After running it, the result is as follows: Tue Sep 08 ...