-1

I have an array that contains variables that are used to control a template.

divisionsListToHide: ['showActivitiesList',
                                'showAssignActionplanDiv',
                                'showPropertiesListDiv',
                                'showAddActivityDiv',
                                'showCurrentActivity',
                                'editCurrentActivity'
                                ],

I want to call a method that will set all variables contained in the divisionsListToHide array to false, i.e.

this.showAssignActionplanDiv = false;
this.showPropertiesListDiv= false; 

I am trying to create a method (function) to accomplish this:

hideDivisions: function()
        {
            for(var i = 0;i<this.divisionsListToHide.length;i++)
            {
                var xx = 'this.' + this.divisionsListToHide[i];  // e.g. this.showActivitiesList
                console.log(xx);                
                eval(xx) = false;


            }
        },

I was hoping to accomplish this through eval(), however, it displays "Uncaught ReferenceError: Invalid left-hand side in assignment". Any suggestion, how to accomplish this?

1
  • Please try searching prior to asking. There are hundreds of similar questions with detailed answers here Commented Jun 4, 2018 at 19:41

1 Answer 1

1

this is an object, and you can access its properties in two ways: this.propertyName and this["propertyName"]. These two ways are the same, except the second one can be used with a variable just like in your case:

var prop = "propertyName";
this[prop] = false;  // is equal to this["propertyName] = false, which is equal to this.propertyName = false

So try this:

hideDivisions: function()
    {
        for (var i = 0; I <this.divisionsListToHide.length; i++)
        {
            this[this.divisionsListToHide[i]] = false;
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for nice explanation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.