8

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.

1 Answer 1

32

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]; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

How to use this variable in attribute binding in angular template

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.