oak99
oak99

Reputation: 49

reference object name with variable - javascript

I have objects representing the days of the week:

var sunday = {
  time7 : "hello",
  time75 : "get up",
  time8 : "brush your teeth"
};
var monday = {
  time7 : "hello",
  time75 : "get up",
  time8 : "brush your teeth"
};

etc...

I then have a function which checks the day and time, and then updates the webpage:

function schedgie() {
  var now = new Date();
  var day = now.getDay();
  var hour = now.getHours();
  var minute = now.getMinutes();
  var dayRange = ["sunday","monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
  var today = dayRange[day];
  if ((hour == "7") && (minute >= "0" && minute <= "29")) {
    document.getElementById("max").innerHTML = today.time7; 
  }
  if ((hour == "7") && (minute >= "30" && minute <= "59")) {
    document.getElementById("max").innerHTML = today.time75;
  }
};

The "today" variable however is not referencing the object name as I hoped. Any assistance would be greatly appreciated. Thank you!

Upvotes: 0

Views: 308

Answers (3)

Ivan86
Ivan86

Reputation: 5708

You can use eval() for this purpose, but it is a dangerous function to use. You might want to think of a different way to organize your data.

E.g. you could have an object days that has keys sunday, monday, etc. In that case it would be as simple as days[today].time75 to get the value you desire.

eval():

The argument of the eval() function is a string. If the string represents an expression, eval() evaluates the expression. If the argument represents one or more JavaScript statements, eval() evaluates the statements.

Warning: Executing JavaScript from a string is an enormous security risk. It is far too easy for a bad actor to run arbitrary code when you use eval().

more here.



Solution with eval():

Note: I put hour == hour in the if statement just so we can prove that it works (we might be in different time zones).

var sunday = {
  time7 : "hello",
  time75 : "get up",
  time8 : "brush your teeth"
};
var monday = {
  time7 : "hello",
  time75 : "get up",
  time8 : "brush your teeth"
};

function schedgie() {
  var now = new Date();
  var day = now.getDay();
  var hour = now.getHours();
  var minute = now.getMinutes();
  var dayRange = ["sunday","monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
  var today = dayRange[day];
  if ((hour == hour) && (minute >= "0" && minute <= "29")) {
    document.getElementById("max").innerHTML = eval(today).time7; 
  }
  if ((hour == hour) && (minute >= "30" && minute <= "59")) {
    document.getElementById("max").innerHTML = eval(today).time75;
  }
};

schedgie();
<div id="max"></div>


A better approach, without eval():

var days = {
  "sunday" : {
    "time7" : "hello",
    "time75" : "get up",
    "time8" : "brush your teeth"
  },
  "monday" : {
    "time7" : "hello",
    "time75" : "get up",
    "time8" : "brush your teeth"
  }
};

console.log( days["sunday"].time7 );
console.log( days["monday"].time8 );

Upvotes: 0

mikado
mikado

Reputation: 128

dayRange as you have it defined is simply an array of strings and has nothing to do with the objects you defined for representing the days of the week.

You need to change the definition of dayRange to

dayRange = [sunday, monday, tuesday, wednesday, thursday, friday, saturday];

Notice the missing "s!

Upvotes: 2

Brian Thompson
Brian Thompson

Reputation: 14355

The value you're accessing from the array is just a string, not the object reference. Just because the string value is the same as the object name doesn't have any special effect.

You might be able to get it working as easily as removing the " " around the weekday names so that they are variables instead of strings. But without knowing the full context of the code I can't say for sure

Upvotes: 1

Related Questions