How to dynamically assign values to variables by vue

for example: I define three variables: data1,data2,data3
and then I want to assign one of them according to the parameter in a function, for example, when the parameter value is 1, assign a value to data1, and a parameter 2, assign a value to data2. This kind, because there may be many kinds of assignment schemes, I think the following kind of if judgment is a little inappropriate, and I did not think of a better method. Please give me some advice from the kind-hearted people who have good methods. Thank you

.
function Test(param) {
    if(param===1){
        this.data1 = ...
    } else if(param===2){
        this.data2 = ...
    } else{
        this.data3 = ...
    }
}

want to achieve an effect similar to the above, but do not use the if.else judgment method

add: I may not be very clear. What I mean is that there may be a lot of parameters and there will be many variables. Is there any way to achieve results like this

?
function Test(param) {
    this.data+param = ...  //
}
Nov.05,2021

write if else and switch as little as possible. It is recommended to take a look at @ waiting for this article: [Analysis] alternatives to if-else and switch in specific scenarios , and books JavaScript Design patterns and Development practices

var obj = {
    a1: '',
    a2: '',
    a3: '',
}
var param = 1; // 2,3
obj['a' + param] = 0;

you can use switch


switch (param) {
    case 1:  this.data1 = ... break;
    case 2:  this.data2 = ... break;
    case 3:  this.data3 = ... break;
}

I don't know what the scene is, and it's not described in much detail
assuming that different param assigns the same value to different data
you can handle it this way

this.data[param] = xxx

I think you can define an object, data structure like this: {param1: data1, param2: data2};
then do this when storing a value:
obj [params1] = data1;
when taking a value:
obj [param1]


your problem should be to dynamically change the parameter values in data, where this is actually the object data, so just write it this way:

this['data' + param] = xxx
Menu