0

I have some Variables that have like a key word and then an identifier and I'm trying to evaluate this expression.

Example:

$day = today; //Assume it is Monday
$.each(responseAdvertiser, function(index, value) {
    var hasMonday = value.hasMonday;     //TRUE OR FALSE
    var hasTuesday = value.hasTuesday;   //TRUE OR FALSE

    //Whar I'm trying to do:
    var has="has";
    var img="img";
    var txt="txt";
    var day = $day.toLowerCase();  //monday
    var hasDay = has+day;          //hasmonday

    if (hasDay == true){          // TRYING TO EVALUATE THIS
        var dayImg= day+img;          //mondayimg
        var dayTxt = day+txt;         //mondaytxt

        var todayImg = value.dayImg;
        var todayTxt = value.dayTxt;
    // I know how to make this work in PHP using variable variables, like
    // $x = hello
    // $hello = buddy
    // echo $$x   #PRINTS buddy
    }
}

I am trying to evaluate hasmonday, but since I'm using the variable "hasDay" which is just a string named "hasmonday" it doesn't really evaluate the way I need to.

Hope I made myself clear, if not, comment and I'll try to explain it better. Thanks in advance.

4
  • if (hasDay === 'hasmonday'){} Commented Aug 29, 2016 at 23:31
  • 1
    you are comparing a string to boolean, it should always return false Commented Aug 29, 2016 at 23:31
  • The problem is that it's not just hasmonday, but I have hastuesday, haswednesday, etc... And I need to evaluate all of this days according to today's weekday Commented Aug 29, 2016 at 23:36
  • No, it's isn't clear what you are try to do. Try restating it a bit. Commented Aug 29, 2016 at 23:36

1 Answer 1

1

To access a property by name, use the brackets notation.

So for your example, you could get the boolean value by

var hasDay = "hasMonday";
var b = value[hasDay]; // equivalent to 'var b = value.hasMonday;'
if (b) {
    // code here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.