js string into variable use

When we use variables to store values ​​in a certain situation, we need to use concatenated strings to concatenate the variables to call the stored value.

My requirement is to generate a few pieces of data in the background at once. I need to store them separately in the object and put them in the array. Since the background code cannot be modified and I cannot push directly when generating, I use this method.

var b2 = '5';
var str = ('b'+2);
console.log(str) //b2
var re =eval('b'+2);
console.log(re) //5

The eval() method is not used much, but it is quite useful in some situations.

Guess you like

Origin blog.csdn.net/tang242424/article/details/111614265