Convert string to variable name in JavaScript

后端 未结 11 1692
无人及你
无人及你 2020-11-22 02:59

I’ve looked for solutions, but couldn’t find any that work.

I have a variable called onlyVideo.

\"onlyVideo\" the string gets passe

11条回答
  •  时光取名叫无心
    2020-11-22 03:40

    let me make it more clear

    function changeStringToVariable(variable, value){
    window[variable]=value
    }
    changeStringToVariable("name", "john doe");
    console.log(name);
    //this outputs: john doe
    let file="newFile";
    changeStringToVariable(file, "text file");
    console.log(newFile);
    //this outputs: text file
    

提交回复
热议问题