Convert string to variable name in JavaScript

后端 未结 11 1559
无人及你
无人及你 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 03:31

    You can do like this

    var name = "foo";
    var value = "Hello foos";
    eval("var "+name+" = '"+value+"';");
    alert(foo);

提交回复
热议问题