Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript dynamically assign a part of variable name

Tags:

typescript

In the typescript, I have the following:

information_1: any;
information_2: any;
information_3: any;

for(var datum in data){                    
    this.information_[datum] = data[datum]; //Of course this is not right.
}

In js, there is a way to dynamically assign a part of variable name. Is there something like that in typescript so that I can have this.information_1 or this.information_2 etc. based on the var datum?

Thanks.

like image 859
Steve Kim Avatar asked Dec 15 '17 04:12

Steve Kim


1 Answers

You certainly can, but you'll need to avoid the dot notation.

[key:string]:any;
    
for(var datum in data){                    
    this['information_' + datum] = data[datum]; 
}
like image 154
Jake Holzinger Avatar answered Dec 09 '22 18:12

Jake Holzinger