Search code examples
javascriptenumstypeconverter

How can I convert my string to type for enums?


For example I have enumtype like: "SampleEnum"

SampleEnum: {
        Day: 0,
        Week: 1,
        Month: 2
    },

But for one spesific stuff, it comes me as string like "SampleEnum" I have to convert it ------> type directly it should be SampleEnum so I can use Enums.SampleEnum. I need this. How can I do ??


Solution

  • SampleEnum= {
        Day: 0,
        Week: 1,
        Month: 2
    }
    
    var str="SampleEnum";
    

    You can access SampleEnum using window object of the browser like

    window[str]