'dynamically select this.variable angular 12
I have problem with selecting variables predefined in component. these are variables:
public editable1: number = 0;
public editable2: number = 0;
public editable3: number = 0;
public editable4: number = 0;
public editable5: number = 0;
next array of arrays:
public editors: []=[
['editable1', 'editable3', 'editable5'],
['editable1', 'editable2', 'editable3']
]
eventually i would like to increment variables according to lists provided in array
this.editors.forEach(element => {
element.forEach(e => {
this['e']++; // this suppose to be pointing to predefined variables, but it does not
});
});
how can I solve it?
edit: provided suggestion is working perfectly, but i want to add additional information about syntax for more complex code. i have changed variable, which have to be edited and now it is inside service.object.object.key:value so it looks like this:
this.editors.forEach(element => {
element.forEach(e => {
(this.service.object as any)[e].key++; // this way I increment value of object, that is inside object, which is in service
});
});
Solution 1:[1]
You can cast your component as any, and then you can access its properties with ['prop_name'] sytnax:
this.editors.forEach((element) => {
element.forEach((e) => {
(this as any)[e]++;
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |