-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?

user761100
  • 2,171
  • 4
  • 23
  • 31

1 Answers1

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;
        }
}
Minderov
  • 521
  • 1
  • 5
  • 20