Convert a string to a variable name in javascript?

后端 未结 3 1402
清歌不尽
清歌不尽 2021-01-19 21:43

Ok you have a set of variables:

var height1 = 10
var height2 = 20
...
var height7 = 70;

The user has given input and you need to get the v

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-19 22:34

    Assign the height's as properties of an object.

    Something like..

    var heightObj = {
            height1 : 10,
            height2 : 20,
            height7 : 70
        };
    
    var hash_div_height_id = "height" + option_name.substring(6);
    

    To access the particular property , use the [] notation to access the property

    var newHeight = heightObj[hash_div_height_id];
    $('a[name=' + hash + ']').closest('[id^="option"]').height(newHeight);
    

提交回复
热议问题